比较指针和整数

时间:2017-06-14 20:41:25

标签: c pointers integer

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

1 个答案:

答案 0 :(得分:2)

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 );