Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstream getline() Problem ...

Reply
Thread Tools

ifstream getline() Problem ...

 
 
Joe
Guest
Posts: n/a
 
      01-21-2004
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
 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      01-21-2004

"Joe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      01-21-2004

"Joe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
Joe
Guest
Posts: n/a
 
      01-22-2004
"Chris Theis" <(E-Mail Removed)> wrote in message news:<bum28q$mtl$(E-Mail Removed)>...
> "Joe" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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
 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      01-22-2004

"Joe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Chris Theis" <(E-Mail Removed)> wrote in message

news:<bum28q$mtl$(E-Mail Removed)>...
> > "Joe" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem opening ifstream twice ... Joe C++ 2 01-20-2004 03:22 PM
Problem with ifstream Alexandros C++ 0 12-26-2003 12:31 PM
ifstream on gcc 3.2 problem, simple question Anton Ishmurzin C++ 7 11-26-2003 01:34 PM
ifstream - read - problem Nils Wogatzky C++ 5 09-08-2003 09:14 PM
ifstream getline() problem John C++ 10 07-14-2003 04:47 PM



Advertisments