Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplained errors due to fopen() and FILE

Tags:

c

file

stdio

fopen

I can't understand the errors that crop up due to the following code.I am trying to open a file using fopen(), but the errors make no sense to me.So please explain the whole thing.

10 - FILE * THE_FILE_YOU_READ;
11 - THE_FILE_YOU_READ = fopen("num.txt","r");

These two line alone produces several warnings and errors,as follows:

11 - warning: data definition has no type or storage class [enabled by default]
11 - error: conflicting types for 'THE_FILE_YOU_READ'
10 - note: previous declaration of 'THE_FILE_YOU_READ' was here
11 - warning: initialization makes integer from pointer without a cast [enabled by default]
11 - error: initializer element is not constant

I'm using Code::Blocks if it's relevant at all. I have not declared this variable `THE_FILE_YOU_READ anywhere else in the program. Any ideas, or is this simply a stupid oversight on my part?

like image 201
user2166234 Avatar asked Nov 23 '25 03:11

user2166234


1 Answers

You've written this code outside of any function, at the file scope. That's invalid. Statements can only occur in functions. Outside of a function, the compiler is trying to interpret the second line as a declaration/definition, with an implicit type of int.

like image 168
R.. GitHub STOP HELPING ICE Avatar answered Nov 25 '25 16:11

R.. GitHub STOP HELPING ICE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!