473,789 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Last record added twice when reading ifstream

Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?

void LoadFile(ifstre am &File, List &List)
{
User lUser;
while(!File.eof ())
{
File >lUser;
List.insert(lUs er,List.nodeCou nt());
}
}

Mar 23 '07 #1
2 3588
On 3/23/2007 1:42 PM, Kenneth wrote:
Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?

void LoadFile(ifstre am &File, List &List)
{
User lUser;
while(!File.eof ())
{
File >lUser;
List.insert(lUs er,List.nodeCou nt());
}
}
Look Here:

http://www.gnomesane.net/code/doc/noteof/

S.
--
Stefan Naewe
stefan_DOT_naew e_AT_atlas_DOT_ de
Mar 23 '07 #2
Kenneth wrote:
Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?

void LoadFile(ifstre am &File, List &List)
{
User lUser;
while(!File.eof ())
{
File >lUser;
List.insert(lUs er,List.nodeCou nt());
}
}
This is an FAQ (I'd even say one of the most F AQs).
It's answered in the C++ FAQ lite at http://www.parashift.com/c++-faq-lite .

Mar 23 '07 #3

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

Similar topics

16
1375
by: Wen | last post by:
Hallo, Can someone tell me why my while doenst read the second, third.... record? (This's a part of my code.) I've looked for it in the books and on internet, but juist don't see the probleem. TI@ Wen int klantNr, access;
12
2075
by: francisds | last post by:
Hi, Can you guys see if there's a solution to this problem? I have a database from which I have to read each record and process that record. New records are being added all the time, so I need to go back and check for new records and process them. However:
15
4664
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any field to start. If I edit that field and then click on the new record button in the navigation buttons, the form goes to a new record and each field has the default value of the previous record. If I put the focus in any field to start, edit that...
12
14935
by: Curten | last post by:
Hi, I'm using ifstream to read data from a txt-file but the last line is always read twice. How Come? The condition for the loop in which the file is read is while(!infile.eof()) so this seems strange to me. Does anyone have a suggestion how to solve this problem?
1
2001
by: empaleador | last post by:
Hi I m reading a file that ends with a blank line and when i do the output it repeats the line before the blank 2 times. I am using ifstream infile; infile.open ("FICHEIROS_DE_ENTRADA.TXT", ifstream::in);
11
8658
by: Timofmars | last post by:
I'm try to Unload DB2 data from a table into a record sequential file on NT. I can an unload on Mainframe, but it doesn't seem to be an option in NT. In NT, all I can do is export/import. I can do a Load too, but not an Unload. I just want the only the data from the table in a record sequential file. Export seems only to give options to have a delimited line sequential file or a record sequential file where the data is preceeded by...
4
7408
by: Rasputin | last post by:
Hello, The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me know for next time. I am reading strings from a file to a vector, doing some "operations" on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last...
7
4011
by: ramana | last post by:
I'm wondering if someone could point me to the flaw in the following code that uses the while(!FP.eof()) condition to read the input data. This condition is reading the last data point of the file twice. #include <iostream> #include <fstream> using namespace std; int main(int argc, char **argv) { double x;
3
6410
by: rutabuga | last post by:
after opening the text file the the prog is soposed to read each record into certain variables and then display the variable name to the screen the problem i am geting is that the last record in the text file is being printed twice the txt file is formated as so 4 Jennifer f n 3 Alexandra f n
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.