Problem opening a file in C

D

Dan

Greetings:

I was trying to read in a file using C, and I got a strange error, compling
it in gcc for Solaris:

Code segment:
#include <stdio.h>
#include <stdlib.h>
.
.
.

FILE *file;
char temp[10];
file = fopen("filelist.txt", "r");
int i = 0;
.
.
.

End Code segment

I got a message saying something along the lines of '(87) parse error before
int'. Anybody have any ideas as to what could be wrong?

Thanks,
Daniel
 
E

E. Robert Tisdale

Dan said:
I was trying to read in a file using C, and I got a strange error, compling
it in gcc for Solaris:

Code segment:
#include <stdio.h>
#include <stdlib.h>
.
.
.

char temp[10];

FILE* file = fopen("filelist.txt", "r");
 
M

Martijn

Artie said:
Dan said:
FILE *file;
char temp[10];
file = fopen("filelist.txt", "r");
int i = 0;

In C89, which your compiler most likely implements, all declarations
must be at the beginning of a block; C99 (and C++) do not have that
restriction[1].

You could, for example, move the declaration of `i' above the call to
fopen().

Or you could make a new, unnamed, segment with a new scope, which will cause
i to be a temporary variable available only to that segment:

file = fopen(szFileName, "r");
{
int i = 0;

// the rest of the segment here
}
 
S

Steve Zimmerman

goose said:
E. Robert Tisdale said:
Dan wrote:

I was trying to read in a file using C, and I got a strange error, compling
it in gcc for Solaris:

Code segment:
#include <stdio.h>
#include <stdlib.h>
.
.
.

char temp[10];
FILE* file = fopen("filelist.txt", "r");

int i = 0;
.
.
.

End Code segment

I got a message saying something along the lines of '(87) parse error before
int'. Anybody have any ideas as to what could be wrong?

?????
could you be less cryptic ? what exactly is that supposed to solve ?
the OP will have *NO* idea why that will work, and he will repost
about the other errors in his code that are similar.

goose,
helpfull today ?

Please post more of your program before the int part that the
compiler complained about. I tried the file open and it
compiled fine for me.
 
D

Dan

That fixed it. Thank you very much for your help.

Artie Gold said:
Dan said:
Greetings:

I was trying to read in a file using C, and I got a strange error, compling
it in gcc for Solaris:

Code segment:
#include <stdio.h>
#include <stdlib.h>
.
.
.

FILE *file;
char temp[10];
file = fopen("filelist.txt", "r");
int i = 0;
.
.
.

End Code segment

I got a message saying something along the lines of '(87) parse error before
int'. Anybody have any ideas as to what could be wrong?

In C89, which your compiler most likely implements, all declarations
must be at the beginning of a block; C99 (and C++) do not have that
restriction[1].

You could, for example, move the declaration of `i' above the call to
fopen().

HTH,
--ag

[1] IMHO, to their detriment
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top