473,394 Members | 1,840 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,394 software developers and data experts.

Read file problem

I would like to write a program to read a text file (data.txt) on
Windows and Linux machines.

data.txt is formated as follows:

1 10 100 1000
2 20
(a new but empty line)
My C++ program is:

***************************************
ifstream fp_input("data.txt");
int a,b;

while(!fp_input.eof())
{
fp_input>>a;

cout<<"a="<<a<<": ";

while(fp_input.peek()!='\n' && fp_input.peek()!='\r' && !
fp_input.eof())
{
fp_input>>b;
cout<<b<<", ";
}

cout<<endl;
}
***************************************

However, the output of my program is:

1: 10 100 1000
2: 20
2:

I don't know why there is an extra "2" in the end of the output.

Could someone give me a clue to modify my program such that it can
display "data.txt" properly under Windows and Linux?

Thanks.

Mar 23 '07 #1
2 3348

<pe*********@gmail.comwrote in message
news:11**********************@e1g2000hsg.googlegro ups.com...
>I would like to write a program to read a text file (data.txt) on
Windows and Linux machines.

data.txt is formated as follows:

1 10 100 1000
2 20
(a new but empty line)
My C++ program is:

***************************************
ifstream fp_input("data.txt");
int a,b;

while(!fp_input.eof())
{
fp_input>>a;

cout<<"a="<<a<<": ";

while(fp_input.peek()!='\n' && fp_input.peek()!='\r' && !
fp_input.eof())
{
fp_input>>b;
cout<<b<<", ";
}

cout<<endl;
}
***************************************

However, the output of my program is:

1: 10 100 1000
2: 20
2:

I don't know why there is an extra "2" in the end of the output.
Because your 'while' loop is incorrect. 'eof()' does not
report true until *after* an attempt to read past end of file.
It does not 'predict' the next read will fail.
>
Could someone give me a clue to modify my program such that it can
display "data.txt" properly under Windows and Linux?
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
int ret(EXIT_SUCCESS);
std::ifstream fp_input("data.txt");

if(fp_input)
{
std::string line;

while(std::getline(fp_input, line))
{
std::istringstream iss(line);
int a(0);

if(iss >a)
{
std::cout << a << ": ";
int b(0);

while(iss >b)
std::cout << b << ", ";

if(!iss.eof())
{
std::cerr << "Error reading input\n";
ret = EXIT_FAILURE;
break;
}

std::cout << '\n';
}
else
{
std::cerr << "Error reading input\n";
ret = EXIT_FAILURE;
break;
}

}
}
else
{
std::cerr << "Cannot open input\n";
ret = EXIT_FAILURE;
}

return ret;
}
Note that if the last line of the input file is empty (contains
only a newline character), parsing it will cause an error to
be reported, since your algorithm unconditionally expects an integer
as the first part of a line (the item you read into object 'a').
You'll either need to eliminate this 'requirement', or remove the
blank line.

-Mike


Mar 23 '07 #2
pe*********@gmail.com wrote:
I would like to write a program to read a text file (data.txt) on
Windows and Linux machines.

data.txt is formated as follows:

1 10 100 1000
2 20
(a new but empty line)
My C++ program is:

***************************************
ifstream fp_input("data.txt");
int a,b;

while(!fp_input.eof())
This usually does not do what you want, since eof() is not set until
*after* a read has failed. See:
http://www.parashift.com/c++-faq-lit....html#faq-15.5
and related questions.

[rest of code snipped]
However, the output of my program is:

1: 10 100 1000
2: 20
2:

I don't know why there is an extra "2" in the end of the output.

Could someone give me a clue to modify my program such that it can
display "data.txt" properly under Windows and Linux?
See above.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
4
by: James Aguilar | last post by:
Hey all, I'm working on an encoding scheme where I am running into a problem with reading a file off a stream. Looking at the binary encoding of the file (using a simple hex editor), there is...
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.