Eyzuky Eyzuky - 4 months ago 14
C Question

C - Opening a file and reading char by char with passing the file pointer as argument

I am having trouble with understanding when i should pass the pointer and when the thing it points at. in my code:

int checkFile(FILE fp)
{
int c;
while((c = fgetc(*fp)) != EOF)
{
putchar(c);
}
fclose(*fp);

}
int main(int argc, char *argv[])
{

FILE *fp = fopen(argv[0], "r");
char fileName = argv[1];
if(argc > 2)
{
printf("Please supply a file!\n");
printf("usage: CheckParenthesis <file name>\n");
}
if (fp == NULL)
{
printf("Error! trying to open the file\n");
return 1;
}
else
{
checkFile(fp);
}
return 0;
}


I get major errors compiling this, the error is:

C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c: In function 'checkFile':
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c:17:22: error: invalid type argument of unary '*' (have 'FILE')
while((c = fgetc(*fp)) != EOF)
^
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c:21:12: error: invalid type argument of unary '*' (have 'FILE')
fclose(*fp);
^
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c: In function 'main':
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c:28:21: warning: initialization makes integer from pointer without a cast
char fileName = argv[1];
^
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c:41:9: error: incompatible type for argument 1 of 'checkFile'
checkFile(fp);
^
C:\Users\Dell\ClionProjects\CheckParenthesis\CheckParenthesis.c:12:5: note: expected 'FILE' but argument is of type 'struct FILE *'


int checkFile(FILE fp)

I know there are several problematics here but i do not know what will be right:
1. Am i opening the right argument? both argv[0] and argv[1] seem to be the file path i specified.. Adding this as a print out i made for getting the argv information:
test:

printf("There are %d args, %s, %s\n", argc,argv[0],argv[1]);


result:

There are 2 args, C:\Users\Dell\.CLion2016.2\system\cmake\generated\CheckParenthesis- 5dc89373\5dc89373\Release\CheckPare
nthesis.exe, C:\testing\brackets.txt



  1. am i doing correct pointer usage?


Answer

Phew! So checkfile() should take a file pointer not a file. Change int checkFile(FILE fp) to int checkFile(FILE* fp) and then later in your while look you should change any *fp to fp

You're code should look like this:

int checkFile(FILE* fp) {
    int c;
    while ((c = fgetc(fp)) != EOF) {
        putchar(c);
    }
    fclose(fp);
}

int main(int argc, char *argv[]) {
    FILE *fp = fopen(argv[0], "r");
    char* fileName = argv[1]; // thanks to dvhh in the comments
    if (argc > 2) {
        printf("Please supply a file!\n");
        printf("usage: CheckParenthesis <file name>\n");
    }
    if (fp == NULL) {
        printf("Error! trying to open the file\n");
        return 1;
    } else {
        checkFile(fp);
    }
    return 0;
}

That should help, also can I see how you're compiling?(assuming you're using gcc)