Problem opening a file in C

Discussion in 'C Programming' started by Dan, Jul 29, 2003.

  1. Dan

    Dan Guest

    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
     
    Dan, Jul 29, 2003
    #1
    1. Advertising

  2. 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?
     
    E. Robert Tisdale, Jul 29, 2003
    #2
    1. Advertising

  3. Dan

    Martijn Guest

    Artie Gold wrote:
    > Dan wrote:
    >> 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
    }

    --
    Martijn Haak
    http://www.serenceconcepts.nl
     
    Martijn, Jul 29, 2003
    #3
  4. goose wrote:

    > "E. Robert Tisdale" <> wrote in message news:<>...
    >
    >>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.
     
    Steve Zimmerman, Jul 29, 2003
    #4
  5. Dan

    Dan Guest

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

    "Artie Gold" <> wrote in message
    news:...
    > Dan wrote:
    > > 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
    >
    > --
    > Artie Gold -- Austin, Texas
    >
     
    Dan, Jul 29, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    10
    Views:
    5,031
    Old Wolf
    Feb 25, 2004
  2. Replies:
    7
    Views:
    525
  3. amacch95

    opening media file in asp file

    amacch95, Apr 17, 2008, in forum: .NET
    Replies:
    0
    Views:
    355
    amacch95
    Apr 17, 2008
  4. fniles
    Replies:
    0
    Views:
    300
    fniles
    Apr 26, 2009
  5. John Smith
    Replies:
    2
    Views:
    304
    Brian McCauley
    Dec 1, 2003
Loading...

Share This Page