Hi...
I've been trying to make a program where the input is txt file, and the output will be saved in txt file. But, I keep getting error Message.
My code looks like this:

#include <stdlib.h>
#include <stdio.h>

int main (){
    int i,j;
    float H [100][100];

FILE*in;
in=fopen("0.txt","rt");
FILE*out;
out=fopen("rotation0.txt","w");
if(in){
  for(i=0;i<100;i++)
    for(j=0;j<100;j++)
      fscanf(in, "%f", &H[i][j]);
  for(i=0;i<100;i++){
    for(j=0;j<100;j++)
      //printf("%d "H[i][j]);
      printf("");;
    }
  }
else {
fprintf(stderr,"Cannot open file\n");
return 1;
}
 for(i=0;i<100;i++){
   for(j=0;j<100;j++){
     printf("[%d][%d]=%f\n ",i,j,H[i][j]);
     fprintf(out,"[%d][%d]=%f\n ",i,j,H[i][j]);
   }
 }

return 0;
fclose(in);
fclose (out);
}

The message says:
"parse error before *"
" "out" undeclared"

I found a couple strange things; if I move FILE*in below FILE*out, the error became:" "in" undeclared". And if I use different compiler (I'm using cygwin currently), it works. unfortunately, I can't use the other compiler because it doesn't work if increase the size of array.

Could you tell me, what's wrong with my code?

Thank you

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.

i don't get salem's argument....i ran ur program on gcc and it's working fine.
no erroe at all.

> 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

commented: Correct! +8

At which line declaration and statement are mixing?

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;
[B]in=fopen("0.txt","rt");[/B]  [B]// you make an assignment here (1)[/B]

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");
commented: :) nicely put +33

thanks , i got ur point.

@salem: thanks for answering my question. It works now. But can I ask you, why did you say that, I'm not going to close files with this:
> return 0;
> fclose(in);
> fclose (out);

Honestly, I didn't get it. I suppose to write this in the end of codes,right?

@tux4life: thank you for your explanation.

*Clarification of Salem's post*
You have to change the order:

return 0;
[B]// The code below won't be executed[/B]
fclose(in);
fclose(out);

My advice is that you should never 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;

:)

Ah, I see it more clearly now...

Thank you for the explanation..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.