in His Steps
in His Steps

Reputation: 3245

fopen C, statement with no effect?

I have one error that I have no idea about... I tried to open file either to write or read and check whether opening was successful. but I get this compilation error... I don't know why;;;

FILE * infile;
....
infile = fopen(filename, "w");
if(!infile)
   return NULL;

and it gives me this error

warning: statement with no effect 
error: expected ';' before 'return'

It is not definately previous messed up semicolons because if I erase that part of code everything works fine. Thank you for your help!

Upvotes: 0

Views: 230

Answers (2)

Graham Perks
Graham Perks

Reputation: 23390

Some things to check:

  • Have you included stdio.h?
  • Is this function supposed to return a value?
  • Is NULL actually defined? (Try returning just 0)

Which line is reporting that warning? Is it really one of the lines you posted?

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126203

Almost certainly the problem is one that you're subconsciously not seeing, so much so that you automatically corrected it when you copied the code to your message above. The error message says that you have a ; immediately before a return, meaning your actual code is almost certainly

if(!infile);
   return NULL;

and the warning is telling you that the if is meaningless, due to the fact that the statement it is guarding is empty (the ; at the end of the if line), and the return will be executed unconditionally.

Upvotes: 3

Related Questions