ifstream getline() Problem ...

J

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
 
C

Chris Theis

Joe said:
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
 
S

Sumit Rajan

Joe said:
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.
 
J

Joe

Chris Theis said:
Joe said:
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
 
C

Chris Theis

Joe said:
"Chris Theis" <[email protected]> wrote in message
Joe said:
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
 

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

Similar Threads

ifstream::getline() synatx 18
Problem opening ifstream twice ... 2
Error with ifstream and exceptions 71
ifstream 5
ifstream/getline 15
About ifstream 2
Filter sober in c++ don't pass test 0
adapting getline 77

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top