472,782 Members | 1,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 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 3308

<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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.