By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

ifstream getline() Problem ...

P: n/a
Joe
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Joe" <un*******@yahoo.com> wrote in message
news:fc**************************@posting.google.c 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.
Jul 22 '05 #2

P: n/a

"Joe" <un*******@yahoo.com> wrote in message
news:fc**************************@posting.google.c 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
Jul 22 '05 #3

P: n/a
Joe
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bu**********@sunnews.cern.ch>...
"Joe" <un*******@yahoo.com> wrote in message
news:fc**************************@posting.google.c 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
Jul 22 '05 #4

P: n/a

"Joe" <un*******@yahoo.com> wrote in message
news:fc**************************@posting.google.c om...
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message

news:<bu**********@sunnews.cern.ch>...
"Joe" <un*******@yahoo.com> wrote in message
news:fc**************************@posting.google.c 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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.