Connecting Tech Pros Worldwide Help | Site Map

Read file problem

  #1  
Old March 23rd, 2007, 09:25 PM
peter.vanna@gmail.com
Guest
 
Posts: n/a
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.

  #2  
Old March 23rd, 2007, 10:05 PM
Mike Wahler
Guest
 
Posts: n/a

re: Read file problem



<peter.vanna@gmail.comwrote in message
news:1174681412.258828.174170@e1g2000hsg.googlegro ups.com...
Quote:
>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.
Quote:
>
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




  #3  
Old March 23rd, 2007, 10:15 PM
Marcus Kwok
Guest
 
Posts: n/a

re: Read file problem


peter.vanna@gmail.com wrote:
Quote:
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]
Quote:
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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting Datagrid to Excel (unable to read file) =?Utf-8?B?Sm9obiBXYWxrZXI=?= answers 3 June 20th, 2007 10:25 PM
read file without locking it. John please don't spam me! answers 4 September 20th, 2006 08:25 AM
read file problem Ernesto answers 4 February 6th, 2006 05:05 PM
Anyway to read file line by line ? Abby answers 40 November 13th, 2005 05:17 PM