Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > this code display text file not correctly

Reply
Thread Tools

this code display text file not correctly

 
 
chat
Guest
Posts: n/a
 
      01-05-2007
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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-05-2007
"chat" <(E-Mail Removed)> wrote in news:1167970544.821629.257510@
38g2000cwa.googlegroups.com:

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

> cout<<a<<endl;


....This line displays whatever a contained the last time around.

>
> }
> 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
>
>


 
Reply With Quote
 
 
 
 
chat
Guest
Posts: n/a
 
      01-05-2007
How to correct this program in order to show result correctly?
thank you very much.
chat watchara

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-05-2007
On 4 Jan 2007 20:19:09 -0800 in comp.lang.c++, "chat"
<(E-Mail Removed)> wrote,
>How to correct this program in order to show result correctly?


while(infile >> a)

 
Reply With Quote
 
chat
Guest
Posts: n/a
 
      01-05-2007

David Harmon เขียน:

> 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

 
Reply With Quote
 
chat
Guest
Posts: n/a
 
      01-05-2007

> David Harmon เขียน:
> > while(infile >> a)


How do you know which variable (infile or a) will used for condition
entering into while loop.

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      01-05-2007
chat wrote:
>> David Harmon เขียน:
>>> 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.
 
Reply With Quote
 
chat
Guest
Posts: n/a
 
      01-05-2007

Sumit Rajan เขียน:

> Please see: http://www.parashift.com/c++-faq-lit....html#faq-15.4
> Regards,
> Sumit.


Thank you very much for your suggestion.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-05-2007
"chat" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> David Harmon เขียน:
> > 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.



 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
treeview node text not displaying correctly NJP ASP .Net 1 03-05-2007 01:49 PM
why does Response.ContentType = "text/plain"; not work correctly? =?Utf-8?B?bGV0dWNlIGRhbmNl?= ASP .Net 3 10-26-2006 06:39 PM
multiple validators on textfield w/display=dynamic don't display correctly cp ASP .Net Web Controls 3 12-24-2004 03:24 PM
Java Applet counter doesn't display correctly Dick Stouffer Firefox 8 10-05-2004 04:52 PM
CHtmlView: How to read html text before display, change and give to display changed html code? Marek Kurowski C Programming 1 08-18-2004 06:41 PM



Advertisments