You can't mix declarations and statements in normal C.
FILE*in;
FILE*out;
in=fopen("0.txt","rt");
out=fopen("rotation0.txt","w");
C++ allows this, C99 (the new C standard, but not widely supported yet) allows this, and so do some compilers as an extension.
> return 0;
> fclose(in);
> fclose (out);
You're not going to close the files with this.
Oh, and work on the indentation as well. Even for a small program, it's already hard to read.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> i don't get salem's argument....i ran ur program on gcc and it's working fine.
Because GCC is one of the compilers which supports C99.
So code which mixes declarations and statements is allowed.
Try with
gcc -std=c89 prog.c
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
At which line declaration and statement are mixing?
Describing the problem:
Take a look at this:
int i,j;
float H [100][100];
FILE*in;
<strong>in=fopen("0.txt","rt");</strong> <strong>// you make an assignment here (1)</strong>
FILE*out;
out=fopen("rotation0.txt","w");
(1): In C89 you aren't allowed to declare any more variables after the first statement (and in this case an assignment statement took place, before the declaration of other variables)
Fixing the problem:
The fix the problem, you'll have to place all declarations before the first statement, like this:
int i,j;
float H [100][100];
FILE*in;
FILE*out;
in=fopen("0.txt","rt");
out=fopen("rotation0.txt","w");
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
If you execute
return 0;
when do you think the file will be closed?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
*Clarification of Salem's post*
You have to change the order:
return 0;
<strong>// The code below won't be executed</strong>
fclose(in);
fclose(out);
My advice is that you shouldnever put code after a return statement, because when a return statement is executed, the function directly exits, so the code after the return statement won't be executed...
How to fix this?
Well, just change the order of the statements, like this:
fclose(in);
fclose(out);
return 0;
:)
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243