ifstream getline() Problem ...

Discussion in 'C++' started by Joe, Jan 21, 2004.

  1. Joe

    Joe Guest

    Hello -

    I wrote a program that uses ifstream to open an ASCII file and
    getline() to read in the lines. The problem is when I try to open the
    same file again later in the code. I used close() to close the file
    but the next open() fails. If I comment out the getline() then I can
    open it a second time without a problem. Here are parts of the code:

    ifstream InputFile;
    char Line[MAXcharLine + 1]; // MAXcharLine = 512

    InputFile.open("X.dat");

    if (InputFile == NULL)
    {
    cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    << endl;

    return false;
    }

    while (InputFile.getline(Line, MAXcharLine))
    {
    // STUFF
    }

    InputFile.close();

    ....

    InputFile.open("X.dat");

    // This here fails!

    if (InputFile == NULL)
    {
    cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    << endl;

    return false;
    }

    Could anybody tell me what's wrong with this code? I use gcc 3.3.1
    under cygwin.

    Thanks,
    Joe
     
    Joe, Jan 21, 2004
    #1
    1. Advertising

  2. Joe

    Chris Theis Guest

    "Joe" <> wrote in message
    news:...
    > Hello -
    >
    > I wrote a program that uses ifstream to open an ASCII file and
    > getline() to read in the lines. The problem is when I try to open the
    > same file again later in the code. I used close() to close the file
    > but the next open() fails. If I comment out the getline() then I can
    > open it a second time without a problem. Here are parts of the code:
    >
    > ifstream InputFile;
    > char Line[MAXcharLine + 1]; // MAXcharLine = 512


    I'd recommend to use a string object instead of a character array here.

    >
    > InputFile.open("X.dat");
    >
    > if (InputFile == NULL)


    Using for example the fopen() function from the C library one could test for
    a NULL pointer to see whether the opening was successful. Using streams the
    mechanism involved is a little different. What you should do is

    if( InputFile ) {

    }

    This works because the streams have implicit conversion functions for void*
    and bool, which check the state of the stream.

    > {
    > cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    > << endl;
    >
    > return false;
    > }
    >
    > while (InputFile.getline(Line, MAXcharLine))


    Changing Line to a string this would become
    while( getline( InputFile, Line) ) { ....
    }

    > {
    > // STUFF
    > }
    >
    > InputFile.close();
    >
    > ...
    >
    > InputFile.open("X.dat");
    >
    > // This here fails!
    >
    > if (InputFile == NULL)


    Change this to
    if( InputFile ) {

    }

    and your problem is solved.

    Regards
    Chris
     
    Chris Theis, Jan 21, 2004
    #2
    1. Advertising

  3. Joe

    Sumit Rajan Guest

    "Joe" <> wrote in message
    news:...
    > Hello -
    >
    > I wrote a program that uses ifstream to open an ASCII file and
    > getline() to read in the lines. The problem is when I try to open the
    > same file again later in the code. I used close() to close the file
    > but the next open() fails. If I comment out the getline() then I can
    > open it a second time without a problem. Here are parts of the code:
    >
    > ifstream InputFile;
    > char Line[MAXcharLine + 1]; // MAXcharLine = 512
    >
    > InputFile.open("X.dat");
    >
    > if (InputFile == NULL)
    > {
    > cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    > << endl;
    >
    > return false;
    > }
    >
    > while (InputFile.getline(Line, MAXcharLine))
    > {
    > // STUFF
    > }
    >
    > InputFile.close();


    You haven't described what exactly happens and what you mean by "fails".

    My guess is that you're trying to reuse a dinner plate without washing it.
    :)

    See if this line (inserted right here: just before the second
    InputFile.open() ) helps:


    InputFile.clear();



    >
    > InputFile.open("X.dat");
    >
    > // This here fails!
    >
    > if (InputFile == NULL)
    > {
    > cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    > << endl;
    >
    > return false;
    > }
    >
    > Could anybody tell me what's wrong with this code? I use gcc 3.3.1
    > under cygwin.
    >


    Why don't you use a std::string (for Line)?

    Regards,
    Sumit.
     
    Sumit Rajan, Jan 21, 2004
    #3
  4. Joe

    Joe Guest

    "Chris Theis" <> wrote in message news:<bum28q$mtl$>...
    > "Joe" <> wrote in message
    > news:...
    > > Hello -
    > >
    > > I wrote a program that uses ifstream to open an ASCII file and
    > > getline() to read in the lines. The problem is when I try to open the
    > > same file again later in the code. I used close() to close the file
    > > but the next open() fails. If I comment out the getline() then I can
    > > open it a second time without a problem. Here are parts of the code:
    > >
    > > ifstream InputFile;
    > > char Line[MAXcharLine + 1]; // MAXcharLine = 512

    >
    > I'd recommend to use a string object instead of a character array here.
    >
    > >
    > > InputFile.open("X.dat");
    > >
    > > if (InputFile == NULL)

    >
    > Using for example the fopen() function from the C library one could test for
    > a NULL pointer to see whether the opening was successful. Using streams the
    > mechanism involved is a little different. What you should do is
    >
    > if( InputFile ) {
    >
    > }
    >
    > This works because the streams have implicit conversion functions for void*
    > and bool, which check the state of the stream.
    >
    > > {
    > > cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    > > << endl;
    > >
    > > return false;
    > > }
    > >
    > > while (InputFile.getline(Line, MAXcharLine))

    >
    > Changing Line to a string this would become
    > while( getline( InputFile, Line) ) { ....
    > }
    >
    > > {
    > > // STUFF
    > > }
    > >
    > > InputFile.close();
    > >
    > > ...
    > >
    > > InputFile.open("X.dat");
    > >
    > > // This here fails!
    > >
    > > if (InputFile == NULL)

    >
    > Change this to
    > if( InputFile ) {
    >
    > }
    >
    > and your problem is solved.
    >
    > Regards
    > Chris


    Hello -

    Thanks a lot for the comments. I guess I didn't know that you have to
    use the clear(). It sounded like the close() would take care of
    everything. Thanks also for the comments regarding the use of string
    and getline(). The reason why I didn't use string was that a long
    time ago my compiler did not have the STL. I guess I should look in
    other places too to see what I can now do easier than I used to.

    Thanks!
    Joe
     
    Joe, Jan 22, 2004
    #4
  5. Joe

    Chris Theis Guest

    "Joe" <> wrote in message
    news:...
    > "Chris Theis" <> wrote in message

    news:<bum28q$mtl$>...
    > > "Joe" <> wrote in message
    > > news:...
    > > > Hello -
    > > >
    > > > I wrote a program that uses ifstream to open an ASCII file and
    > > > getline() to read in the lines. The problem is when I try to open the
    > > > same file again later in the code. I used close() to close the file
    > > > but the next open() fails. If I comment out the getline() then I can
    > > > open it a second time without a problem. Here are parts of the code:
    > > >
    > > > ifstream InputFile;
    > > > char Line[MAXcharLine + 1]; // MAXcharLine = 512

    > >
    > > I'd recommend to use a string object instead of a character array here.
    > >
    > > >
    > > > InputFile.open("X.dat");
    > > >
    > > > if (InputFile == NULL)

    > >
    > > Using for example the fopen() function from the C library one could test

    for
    > > a NULL pointer to see whether the opening was successful. Using streams

    the
    > > mechanism involved is a little different. What you should do is
    > >
    > > if( InputFile ) {
    > >
    > > }
    > >
    > > This works because the streams have implicit conversion functions for

    void*
    > > and bool, which check the state of the stream.
    > >
    > > > {
    > > > cerr << "Error at " << __LINE__ << ": Could not open file 'X.dat'"
    > > > << endl;
    > > >
    > > > return false;
    > > > }
    > > >
    > > > while (InputFile.getline(Line, MAXcharLine))

    > >
    > > Changing Line to a string this would become
    > > while( getline( InputFile, Line) ) { ....
    > > }
    > >
    > > > {
    > > > // STUFF
    > > > }
    > > >
    > > > InputFile.close();
    > > >
    > > > ...
    > > >
    > > > InputFile.open("X.dat");
    > > >
    > > > // This here fails!
    > > >
    > > > if (InputFile == NULL)

    > >
    > > Change this to
    > > if( InputFile ) {
    > >
    > > }
    > >
    > > and your problem is solved.
    > >
    > > Regards
    > > Chris

    >
    > Hello -
    >
    > Thanks a lot for the comments. I guess I didn't know that you have to
    > use the clear(). It sounded like the close() would take care of
    > everything. Thanks also for the comments regarding the use of string
    > and getline(). The reason why I didn't use string was that a long
    > time ago my compiler did not have the STL. I guess I should look in
    > other places too to see what I can now do easier than I used to.
    >
    > Thanks!
    > Joe


    Nowadays the formerly called STL is part of standard C++ and known as the
    standard library. I'd recommend to get a copy of "The C++ standard library"
    by N. Josuttis. There you will find a lot of things that will make your life
    easier.

    Cheers
    Chris
     
    Chris Theis, Jan 22, 2004
    #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. John
    Replies:
    10
    Views:
    10,059
    Buster Copley
    Jul 14, 2003
  2. Jim Phelps
    Replies:
    1
    Views:
    1,729
    Karl Heinz Buchegger
    Jan 21, 2004
  3. Christopher Benson-Manica

    ifstream/getline

    Christopher Benson-Manica, Apr 1, 2004, in forum: C++
    Replies:
    15
    Views:
    28,541
    Leor Zolman
    Apr 1, 2004
  4. tinks
    Replies:
    1
    Views:
    625
  5. Assertor
    Replies:
    2
    Views:
    2,580
    Assertor
    Feb 26, 2006
Loading...

Share This Page