Connecting Tech Pros Worldwide Help | Site Map

this code display text file not correctly

  #1  
Old January 5th, 2007, 04:25 AM
chat
Guest
 
Posts: n/a
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

  #2  
Old January 5th, 2007, 04:25 AM
chat
Guest
 
Posts: n/a

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

  #3  
Old January 5th, 2007, 04:25 AM
Andre Kostur
Guest
 
Posts: n/a

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
>
>
  #4  
Old January 5th, 2007, 05:05 AM
David Harmon
Guest
 
Posts: n/a

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)

  #5  
Old January 5th, 2007, 05:35 AM
chat
Guest
 
Posts: n/a

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

  #6  
Old January 5th, 2007, 05:45 AM
chat
Guest
 
Posts: n/a

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.

  #7  
Old January 5th, 2007, 06:15 AM
Sumit Rajan
Guest
 
Posts: n/a

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.
  #8  
Old January 5th, 2007, 06:35 AM
chat
Guest
 
Posts: n/a

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.

  #9  
Old January 5th, 2007, 08:25 PM
Jim Langston
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set up Display on status line with Threading tshad answers 9 October 13th, 2008 07:25 AM
cant find the problem with my text file cv87cv answers 2 December 14th, 2006 03:49 PM
Filling up a listview dynamically with file contents VM answers 6 November 15th, 2005 12:27 PM
format a retrieved text file John answers 5 July 19th, 2005 06:20 AM