P
peter.vanna
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.
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.