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

Problems with ifstream and getline

P: n/a
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 record length flat file
that is generated by an old IBM mainframe. These data fields need to
be read in by my program EXACTLY as they are represented in the file.
Here is an example text file that I used in my test.

aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbb bbcccccccccccccccccccccccccc
ddddddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeee eeffffffffffffffffffffffffff
gggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhh hhiiiiiiiiiiiiiiiiiiiiiiiiii

For the purposes of this test, I have made each "field" 26 characters
in length, and three fields exist per line (record). I produced the
following code in which to pull the first three fields from the first
record in the file.

#include <stdlib.h>
#include <fstream.h>

int main (int argc, char* args[])
{
ifstream fReader;

char field1[27];
char field2[27];
char field3[27];

fReader.open("data.txt", ios::in );

if (!fReader)
{
cout << "Unable to open file!!!" << endl;
return (1);
}

fReader.getline(field1, 27);
fReader.getline(field2, 27);
fReader.getline(field3, 27);

cout << "Field 1: " << field1 << endl;
cout << "Field 2: " << field2 << endl;
cout << "Field 3: " << field3 << endl;

if (fReader.eof())
{
cout << "End of file reached." << endl;
}

if (!fReader)
{
cout << "File error" << endl;
return (1);
}

fReader.close();

return (0);
}

I compiled this code using the CC compiler in Sun Workshop 6.2. No
special compiler flags were included in the compile. The output of
the program is the following.

Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
Field 2:
Field 3:
File error

As you can see, it seems that after the first getline, the ifstream
loses it's way as the next two getlines retreive no information at
all. Doing the check of the ifstream at the end of the program
indicates that there is a problem.

What exactly am I doing wrong? This problem has driven me up the wall
for the past two days, and I need a fresh set of eyes to shed some
light on the problem.

Thanks in advance,
jp
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jim Phelps wrote:

I compiled this code using the CC compiler in Sun Workshop 6.2. No
special compiler flags were included in the compile. The output of
the program is the following.

Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
Field 2:
Field 3:
File error


Cut&Paste to VC++ 6.0
compiled, linked, run.

The Output is:

Field 1: aaaaaaaaaaaaaaaaaaaaaaaaa
Field 2: bbbbbbbbbbbbbbbbbbbbbbbbb
Field 3: ccccccccccccccccccccccccc

Although this proves not much I take it as a strong indication
that something is wrong with your compiler.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.