-4

I have a question, I tried to copy one line from a pointer to file but there is one error that says that I cannot compare a interger with a pointer, can anyone help me? The error is in line ch = getc(file1); and 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);   
        }
SiggiSv
  • 1,219
  • 1
  • 10
  • 20
  • 1
    `getc` etc return an `int`, not a `char` intentionally! `while(ch != EOF){` with `char ch;` is soo very wrong! And check the result of functions like `fopen`, etc! – too honest for this site Jun 14 '17 at 20:43
  • You declared `ch` as a `char *` instead of `char` – Patrick Roberts Jun 14 '17 at 20:43
  • 1
    @PatrickRoberts It is not acceptable to store the return value of `getc` in a `char` because it loses information. – melpomene Jun 14 '17 at 20:45
  • 1
    @PatrickRoberts: A `char` can either not hold `EOF`, or it will cause trouble for certain valid character codes, depending on the signed-ness of `char`! Maybe you also want to recap C's stream functions. and `EOF`? Either way, it is plain wrong. – too honest for this site Jun 14 '17 at 20:46
  • 1
    @PatrickRoberts From your own link: "*This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.*" Either `char` is an unsigned type on your platform, then `EOF` is out of range; or `char` is a signed type, then half of all valid characters are out of range. – melpomene Jun 14 '17 at 20:48
  • 2
    @PatrickRoberts No that's not correct. This is the third person telling you so. `EOF` cannot fit in a char. – kaylum Jun 14 '17 at 20:48
  • 1
    @PatrickRoberts `EOF` is not guaranteed to be `-1`, and `char` is not guaranteed to be able to store negative values. Check your sources etc. – melpomene Jun 14 '17 at 20:53
  • @PatrickRoberts So ask yourself this. Why does `getc` return `int` and not `char`? Were the original authors just missing the point or are you? But yes, I do agree with you that the question has a different initial problem. – kaylum Jun 14 '17 at 20:54
  • 1
    @PatrickRoberts You missed the second half of my other comment: On the platform you're testing, `char` is a signed type. That means it can't store the values `128` .. `255`, which can be returned by`getc`. In practice the likely effect is that `255` will turn into `-1` and be confused for EOF. – melpomene Jun 14 '17 at 20:56
  • @PatrickRoberts [here I show how storing the return value of getchar is going to fail in various scenarios](https://stackoverflow.com/a/35356684/918959) – Antti Haapala -- Слава Україні Jun 14 '17 at 20:56

1 Answers1

2

Change the type of ch from char * to int.

7.21.7.5 The getc function

Synopsis

1    #include <stdio.h>
      int getc(FILE *stream);

Description

2    The getc function is equivalent to fgetc, except that if it is implemented as a macro, it may evaluate stream more than once, so the argument should never be an expression with side effects.

Returns

3    The getc function returns the next character from the input stream pointed to by stream. If the stream is at end-of-file, the end-of-file indicator for the stream is set and getc returns EOF. If a read error occurs, the error indicator for the stream is set and getc returns EOF.

C 2011 Standard, Online Draft

You will need to use %c instead of %s to print out ch; additionally, the following will result in an infinite loop

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

because you're not updating ch in the body of the loop. Change that to

while ( ( ch = getc( file1 ) ) != EOF )
  printf( "%c", ch );
John Bode
  • 119,563
  • 19
  • 122
  • 198