Problems with ifstream and getline

J

Jim Phelps

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.

aaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccc
ddddddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffff
gggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiii

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
 
K

Karl Heinz Buchegger

Jim said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top