I am learning C and this might be a trival question to some.
My IDE complaints that there is an error: expected expression before ‘!=’ token in the while loop.
I can’t see any problem with that, any suggestions?
/* Print a file to the console, line by line. */
FILE *fp_infile;
char linebuffer[512];
if (( fp_infile=fopen("input.dat", "r")) == NULL )
{
fprintf(stderr, "Couldn't open input file.\n");
return -1;
}
while ( fgets( linebuffer, sizeof(linebuffer), fp_infile )) != NULL ) // ERROR
fputs( linebuffer, stdout );
if ( ! feof(fp_infile) ) // This means "if not end of file"
fprintf( stderr, "Error reading from input file.\n" );
if ( fclose(fp_infile) != 0 )
{
fprintf(stderr, "Error closing input file.\n");
return -2;
}
You have a parenthesis mismatch error. Count the number of
(and compare to the number of).The line should probably be:
Note that you only need parens with
sizeofwhen required by the argument’s syntax;sizeofitself is an operator, not a function.My manual and very kindergarten-like way of matching parenthesis, is to simply scan the line from left to right, while maintaining a count:
(, increase the count), decrease the countWhen you reach the end of the line, if the count isn’t back to 0, you have a mismatch. Very obvious, but quite efficient to quickly determine the existence of a mismatch.