473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jan 5 '07 #1
8 2268
How to correct this program in order to show result correctly?
thank you very much.
chat watchara

Jan 5 '07 #2
"chat" <ch***********@gmail.comwrote 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

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

Jan 5 '07 #4

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

Jan 5 '07 #5
David Harmon à¢Õ¹:
while(infile >a)
How do you know which variable (infile or a) will used for condition
entering into while loop.

Jan 5 '07 #6
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.
Jan 5 '07 #7

Sumit Rajan à¢Õ¹:
Please see: http://www.parashift.com/c++-faq-lit....html#faq-15.4
Regards,
Sumit.
Thank you very much for your suggestion.

Jan 5 '07 #8
"chat" <ch***********@gmail.comwrote in message
news:11**********************@s80g2000cwa.googlegr 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.

Jan 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/authoring-faq Posting-Frequency: twice a week (Mondays and Thursdays) Last-modified: August 28, 2002 Version: 1.15 URL: http://css.nu/faq/ciwas-aFAQ.html...
0
by: Jan Roland Eriksson | last post by:
Archive-name: www/stylesheets/authoring-faq Posting-Frequency: twice a week (Mondays and Thursdays) Last-modified: April 10, 2003 Version: 1.16 URL: http://css.nu/faq/ciwas-aFAQ.html Maintainer:...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
24
by: Adam | last post by:
Hi, Does anyone know of an online C-to-HTML converter to display code nicely formatted in a browser? Thanks a lot, Adam
2
by: JimmyKoolPantz | last post by:
We purchased som software for encoding a barcode. We want to automate the process of converting a number to a readable barcode. However, I am having a few issues. The file that the barcode...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.