Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 5th, 2007, 04:25 AM
chat
Guest
 
Posts: n/a
Default this code display text file not correctly

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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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.



 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles