-4
我有一個問題,我試圖從指針複製一行到文件,但有一個錯誤,說我不能比較指針的整數,任何人都可以幫助我嗎?該錯誤是在管線和ch = getc(file1);
while(ch != EOF)
指針和整數之間的比較
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#define GetCurrentDir getcwd //get the path of file
#define BUFFER_LEN 1024
int main(){
char cCurrentPath[FILENAME_MAX]; //get
char line[BUFFER_LEN]; //get command line
char* argv[100]; //user command
char* path= "/bin/"; //set path at bin
char *ch;
char progpath[20]; //full file path
int argc; //arg count
FILE *file1, *file2; //Files for history
int delete_line, count=0; //line to delete and counter
while(1){
file1 = fopen("fileOne","w");
if(GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
printf("%s",cCurrentPath);
}
printf("/SimpleShell>> "); //print shell prompt
if(!fgets(line, BUFFER_LEN, stdin))
{ //get command and put it in line
break; //if user hits CTRL+D break
}
else if(line, BUFFER_LEN, SIGQUIT){
fopen("fileOne.c","r");
ch = getc(file1);
while(ch != EOF){
printf("%s",ch);
}
}
if(count<20)
{
fputs(argv[100] ,file1);
}
else{
fclose(file1);
file1 = fopen("fileOne.c","r");
rewind(file1);
file2 = fopen("repicla.c","w");
ch = getc(file1);
while(ch != EOF){
ch = getc(file1);
if(ch != "\n"){
count++;
if(count != 20){
putc(ch, file2);
}
}
}
fclose(file1);
fclose(file2);
remove("fileOne.c");
rename("replica.c","fileOne.c");
fputs(argv[100] ,file1);
}
'getc'等返回一個'int',而不是''字符'故意! 'while(ch!= EOF){'with'char ch;'太不對了!並檢查像'fopen'等功能的結果! – Olaf
您將'ch'聲明爲'char *'而不是'char' –
@PatrickRoberts將'getc'的返回值存儲在'char'中是不可接受的,因爲它丟失了信息。 – melpomene