FILE* c,d; doesn't work!

1
2
3
FILE* c,d;
c=fopen("C:\\chara.txt","r");
d=fopen("C:\\inta.txt","w");


1
2
3
4
FILE* c;
c=fopen("C:\\chara.txt","r");
FILE* d;
d=fopen("C:\\inta.txt","w");


The first code can't be compiled. The second do. Why is that?
when you declare pointers you have to repeat '*'
for example, int* a, b; now a's type is int* and b's type is int.
int* a, *b; both a and b are pointers.
hello..well of course it does not compile...try to think simple..to open a file,u need one pointer.a FILE pointer exactly..now to open 2 files u need ??? 2 FILE pointers..no??
So as you already now the second is correct..but in the first one the declaration is incorrect.
Why?..well because to declare 2 pointers,of any kind(int, float,FILE) u need to write:
1
2
3
int *a,*b;     //or
float *a,*g; //or in you're  case
FILE *c,*d;

And as a "tip" try to don't write like this FILE* c;
but write FILE *c
"stick" together the '*' and the name of the variable,because(for example) the variable c and *c are NOT the same. the Wildcard Operator (*) is part of the pointer;and that way you don't get confused..hope it helps :)

sry for repeating what hamsterman said ..but i was typing when he posted and i did not see..but sometimes 2 advices are better that one
Last edited on
Thank you :)

An other question is what does this:

FILE a;

mean?

Is it usable?
That to be honnest i don't now yet :).i am a beginner like you..but if you work with files u need pointers..try to read more about files.u can find a lot of things in the reference menu.
Why wouldn't it be. However fopen returns FILE*, so if you wanted to use FILE, you'd have to do
1
2
3
4
FILE* file_ptr = fopen(...);
FILE file;
if(file_ptr) file = *file_ptr;
else //error! 

And there is really no reason why you would want to do that.
Thank you!
Topic archived. No new replies allowed.