473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Problems with ifstream and getline

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

Similar topics

5
by: Pete H | last post by:
I am trying to use the same ifstream object to open two files. I will eventually want to open many with a loop. The first file opens fine, but the second has a problem. In my test I try to open two...
4
by: Joe | last post by:
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()...
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...
3
by: Eric Lilja | last post by:
Hello, I'm creating a small utility for an online game. It involves parsing a text file of "tradesskill recipes" and inserting these recipes in a gui tree widget (similar to gui file browsers if...
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); //...
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...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.