954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

input and output file (txt file)

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

mingke
Newbie Poster
4 posts since May 2009
Reputation Points: 41
Solved Threads: 0
 

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
Team Colleague
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.
no erroe at all.

Pavan_
Light Poster
29 posts since Apr 2009
Reputation Points: 26
Solved Threads: 3
 

> 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
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

At which line declaration and statement are mixing?

Pavan_
Light Poster
29 posts since Apr 2009
Reputation Points: 26
Solved Threads: 3
 
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
 

thanks , i got ur point.

Pavan_
Light Poster
29 posts since Apr 2009
Reputation Points: 26
Solved Threads: 3
 

@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.

mingke
Newbie Poster
4 posts since May 2009
Reputation Points: 41
Solved Threads: 0
 

If you execute
return 0;
when do you think the file will be closed?

Salem
Posting Sage
Team Colleague
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
 

Ah, I see it more clearly now...

Thank you for the explanation..

mingke
Newbie Poster
4 posts since May 2009
Reputation Points: 41
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You