Connecting Tech Pros Worldwide Help | Site Map

this code display text file not correctly

chat
Guest
 
Posts: n/a
#1: Jan 5 '07
Hi,

I write program for writing and reading text file like this

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("test.txt");
for(int i=0;i<5;i++) outfile<<i<<endl;
outfile.close();

ifstream infile("test.txt");
int a;
while(infile) {
infile>>a;
cout<<a<<endl;

}
infile.close(); return 0;
}

The result is this
0
1
2
3
4
4

Can anybody tell me what is the mistake in my program. It should show 0
1 2 3 4 (only one 4).
Why does it show digit 4 two times? (this code was compile with vc++ 6)
Thank you in advance.
chat watchara

chat
Guest
 
Posts: n/a
#2: Jan 5 '07

re: this code display text file not correctly


How to correct this program in order to show result correctly?
thank you very much.
chat watchara

Andre Kostur
Guest
 
Posts: n/a
#3: Jan 5 '07

re: this code display text file not correctly


"chat" <chat.watchara@gmail.comwrote in news:1167970544.821629.257510@
38g2000cwa.googlegroups.com:
Quote:
Hi,
>
I write program for writing and reading text file like this
>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("test.txt");
for(int i=0;i<5;i++) outfile<<i<<endl;
outfile.close();
>
ifstream infile("test.txt");
int a;
while(infile) {
infile>>a;
You don't check to see if this extraction succeeded. If it fails...
Quote:
cout<<a<<endl;
....This line displays whatever a contained the last time around.
Quote:
>
}
infile.close(); return 0;
}
>
The result is this
0
1
2
3
4
4
>
Can anybody tell me what is the mistake in my program. It should show 0
1 2 3 4 (only one 4).
Why does it show digit 4 two times? (this code was compile with vc++ 6)
Thank you in advance.
chat watchara
>
>
David Harmon
Guest
 
Posts: n/a
#4: Jan 5 '07

re: this code display text file not correctly


On 4 Jan 2007 20:19:09 -0800 in comp.lang.c++, "chat"
<chat.watchara@gmail.comwrote,
Quote:
>How to correct this program in order to show result correctly?
while(infile >a)

chat
Guest
 
Posts: n/a
#5: Jan 5 '07

re: this code display text file not correctly



David Harmon เขียน:
Quote:
while(infile >a)
You are cool. Your suggestion work well. thank you very much.
But I still want to know why my program does not work?

chat watchara

chat
Guest
 
Posts: n/a
#6: Jan 5 '07

re: this code display text file not correctly


Quote:
David Harmon เขียน:
Quote:
while(infile >a)
How do you know which variable (infile or a) will used for condition
entering into while loop.

Sumit Rajan
Guest
 
Posts: n/a
#7: Jan 5 '07

re: this code display text file not correctly


chat wrote:
Quote:
Quote:
>David Harmon เขียน:
Quote:
>>while(infile >a)
>
How do you know which variable (infile or a) will used for condition
entering into while loop.
>
Please see: http://www.parashift.com/c++-faq-lit....html#faq-15.4

Regards,
Sumit.
chat
Guest
 
Posts: n/a
#8: Jan 5 '07

re: this code display text file not correctly



Sumit Rajan เขียน:
Quote:
Please see: http://www.parashift.com/c++-faq-lit....html#faq-15.4
Regards,
Sumit.
Thank you very much for your suggestion.

Jim Langston
Guest
 
Posts: n/a
#9: Jan 5 '07

re: this code display text file not correctly


"chat" <chat.watchara@gmail.comwrote in message
news:1167975323.425062.277330@s80g2000cwa.googlegr oups.com...
Quote:
David Harmon เขียน:
Quote:
while(infile >a)
How do you know which variable (infile or a) will used for condition
entering into while loop.

intfile >a
returns a stream reference (istream I think, maybe something else) which has
a bool override so can be used as a bool in an if statement which returns
false if the stream is in a bad state. It will be in a bad state if some
conditions occur, such as trying to read "x" into an int, or hitting the end
of file.



Closed Thread