473,473 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ifstream getline() Problem ...

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
4 3673

"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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
1
by: Jim Phelps | last post by:
Hello all, I am in a bit of a pickle using the getline function with an ifstream. It does not seem to work as advertised. Here is my scenario. In a nutshell, my code needs to pick up a fixed...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
1
by: tinks | last post by:
I am getting a linking error when I do something like this: ifstream dataFile; dataFile.open(dataFileName_, ios::in); while(dataFile) { dataFile.getline(buffer, MAX_DATA_FILE_LINE_LEN); //...
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
2
by: manwanirg | last post by:
the function getline is a public member of istream and cin.getline can be used. Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris...
3
by: toton | last post by:
Hi, I want to unread a few things while I am reading a file. One solution I know is putback the characters read to the buffer. But as I need frequent moving file pointer , to a few steps back, I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.