what is the diference between the two statements and which is the correct one and what should be used?
fp is the file pointer used.
if(fp!= '\n')
getting a warning
C4047: '!=' : 'FILE *' differs in levels of indirection from 'int'
and
if(fp!= NULL)
Not getting any warnings.
Full code snippet as follows
if(fp!= NULL)
{
fgets(mystring,BUFSIZ,fp);
puts(mystring);
}
About the difference:
fp != '\n'tries to compare a pointer with an integer. This is meaningless.fp != NULLchecks whetherfpis a null pointer. This is a sensible thing to do.About which is the correct one:
This question assumes that one of them actually is correct. (But
fp != '\n'is definitely incorrect.)About what should be used:
That depends on what you’re trying to do.
This code:
is also buggy because you’re ignoring the return value of
fgets, which would tell you whetherfgetssucceeded and hence whethermystringwas set to a valid string.