我有一个问题,我试图从指针复制一行到文件,但是出现一个错误,表明我无法将指针与指针进行比较,有人可以帮助我吗?错误在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);
        }

最佳答案

ch的类型从char *更改为int


7.21.7.5 getc函数
概要
1 #include <stdio.h>
int getc(FILE *stream);
描述
2 getc函数与fgetc等效,不同之处在于如果将其实现为宏,则
可能会多次评估stream,因此该参数永远不应是表达式
有副作用。
退货
3 getc函数从输入指向的输入流中返回下一个字符
stream。如果流位于文件末尾,则设置该流的文件末尾指示符,然后
getc返回EOF。如果发生读取错误,则设置流的错误指示符,并
getc返回EOF


C 2011 Standard, Online Draft

您将需要使用%c而不是%s来打印ch。此外,以下内容将导致无限循环

ch = getc(file1);
while(ch != EOF){
    printf("%s",ch);
}


因为您没有在循环体中更新ch。更改为

while ( ( ch = getc( file1 ) ) != EOF )
  printf( "%c", ch );

关于c - 比较指针和整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44554382/

10-11 23:17