Compare integer and pointer

Asked

Viewed 1,079 times

0

Well, I have a question, I’m trying to copy a line to a file but there’s an error that says I can’t compare an integer to a Pointer, can anyone help me? The error is on the 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);   
    }

1 answer

0

getc() returns a int, not a char *. In this int is the ASCII value of the next character in the file. And, really, when you say while (ch != EOF), you are comparing the value of that character with the value EOF that marks the end of the file, that is, there are no more characters to recover.

Then you must declare ch as int, nay char *. So your program should work properly.

  • Thanks, I understood the explanation but even so this line of code gives the same error if(ch != ' n')

  • In the code, it’s if (ch!="\n"); note double quotes, not simple. This causes "\n" be the type const char [], so he’s trying to compare int with char *.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.