473,386 Members | 1,864 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,386 software developers and data experts.

problem with file

Why does this stupid code read twice last number??? Tnx

#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

int main ( )
{

int number;
ofstream testfile ( "test.txt" , ios::app);

cout<<"Insert a number, ctrl+z to end"<<endl;

while(cin>>number){
testfile<<number<<endl;
cout<<"Insert a number, ctrl + z to end"<<endl;
}

testfile.close();
cout<<"Reading number(s)from file..."<<endl;

ifstream testfile2 ("test.txt" , ios::in );

while(!testfile2.eof()){
testfile2>>number;
cout<<number<<endl;
}

system ("pause");
}

Jul 22 '05 #1
6 1036
Paolo wrote:
Why does this stupid code read twice last number??? Tnx
IIRC, there was a bug in Microsoft's I/O code. Try looking in the
archives for "read last line twice" or something like that.
[...]


V
Jul 22 '05 #2
Paolo wrote:
Why does this stupid code read twice last number???


Probably because it is, as you said, stupid! Of course,
you might find out the yourself as this question was
answered quite frequently but here is key problem anyway:
The stream does not know a priori whether an operation
will fail. Thus, you should always check the stream after
your operation: you did it right in the first loop, you
did it wrong in the second.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #3
Il Fri, 14 Jan 2005 11:11:47 -0500, Victor Bazarov
<v.********@comAcast.net> ha scritto:
Paolo wrote:
Why does this stupid code read twice last number??? Tnx


IIRC, there was a bug in Microsoft's I/O code. Try looking in the
archives for "read last line twice" or something like that.


Thank you, I solved :)
Jul 22 '05 #4
Il 14 Jan 2005 08:15:37 -0800, "Dietmar Kuehl"
<di***********@yahoo.com> ha scritto:
Paolo wrote:
Why does this stupid code read twice last number???


Probably because it is, as you said, stupid! Of course,
you might find out the yourself as this question was
answered quite frequently but here is key problem anyway:
The stream does not know a priori whether an operation
will fail. Thus, you should always check the stream after
your operation: you did it right in the first loop, you
did it wrong in the second.


Thank u, solved in this way:

while(testfile2>>number) {
cout<<number<<endl;
}
Jul 22 '05 #5
On Fri, 14 Jan 2005 17:04:11 +0100 in comp.lang.c++, Paolo
<gh******************@hotmail.com> wrote,
Why does this stupid code read twice last number??? Tnx ....while(!testfile2.eof()){


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[15.5] Why does my input seem to process past the end of file?" It is
always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 22 '05 #6
Il Sat, 15 Jan 2005 19:03:36 GMT, David Harmon <so****@netcom.com> ha
scritto:

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[15.5] Why does my input seem to process past the end of file?" It is
always good to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/


Thank you, these FAQs are very useful ;)
I don't know about them (how to get them in particular), sorry again!
Jul 22 '05 #7

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

Similar topics

7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
12
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too....
15
by: Ken Allen | last post by:
I have been developing a suite of assemblies over the past couple of weeks, and this afternoon somethign started misbehaving. If I do not run the IDE and compiler the code from the command line,...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
13
by: Lee Newson | last post by:
Hi, I have just written my first application using VB.NET. The app works fine when i am running it within .NET for debugging purposes, however when i try to run the app from the .exe file that...
2
by: key9 | last post by:
Hi all on last post I confused on how to organize file of class, ok ,the problem solved : should include class define head on cpp file. but this time ,still link error: strange is I put the...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
4
by: Salad | last post by:
I have a situation where some, not all, users get the message "Couldn't find file "F:\AccessApps\AppName.mdw". This file is required for startup". My app the users are attempting to access is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.