473,803 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Last line of txt-file read twice with ifstream

13 New Member
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.e of())

so this seems strange to me. Does anyone have a suggestion how to solve this problem?
Oct 5 '06 #1
12 14941
gasfusion
59 New Member
hmm...it should work, can you post the text file?
Oct 5 '06 #2
Curten
13 New Member
Sure, the txt-file looks like this:

Expand|Select|Wrap|Line Numbers
  1. GLU   4    3.77
  2.    GLU  17    3.67
  3.    GLU  13    3.84
  4.    GLU  21    3.32
  5.    C-   30    2.74
  6.    HIS   5    7.42
  7.    HIS  10    7.23
  8.    CYS   6   99.99
  9.    CYS   7   99.99
  10.    CYS  11   99.99
  11.    CYS  20   99.99
  12.    CYS   7   99.99
  13.    CYS  19   99.99
  14.    TYR  14   10.07
  15.    TYR  19    9.92
  16.    TYR  16   10.07
  17.    TYR  26   10.07
  18.    N+    1    7.86
  19.    LYS  29    9.73
  20.    ARG  22   11.38
I put the data in an array of structs{char[4], int, float} and everything works fine except that the last line is read (and stored) twice...
Oct 5 '06 #3
r035198x
13,262 MVP
Sure, the txt-file looks like this:

Expand|Select|Wrap|Line Numbers
  1. GLU   4    3.77
  2.    GLU  17    3.67
  3.    GLU  13    3.84
  4.    GLU  21    3.32
  5.    C-   30    2.74
  6.    HIS   5    7.42
  7.    HIS  10    7.23
  8.    CYS   6   99.99
  9.    CYS   7   99.99
  10.    CYS  11   99.99
  11.    CYS  20   99.99
  12.    CYS   7   99.99
  13.    CYS  19   99.99
  14.    TYR  14   10.07
  15.    TYR  19    9.92
  16.    TYR  16   10.07
  17.    TYR  26   10.07
  18.    N+    1    7.86
  19.    LYS  29    9.73
  20.    ARG  22   11.38
I put the data in an array of structs{char[4], int, float} and everything works fine except that the last line is read (and stored) twice...
are you not doing something with that line after the while loop?
If not, you may need to post the whole while loop.
Oct 5 '06 #4
Curten
13 New Member
This is the entire loop:

Expand|Select|Wrap|Line Numbers
  1.         while (!infile.eof())
  2.         {
  3.                 infile>>ws;
  4.                 if (isalpha(infile.peek())){
  5.                         infile>>Data.Name;
  6.                 }
  7.  
  8.                 infile>>ws;
  9.                 if (isdigit(infile.peek())){
  10.                         infile>>Data.Index;
  11.                 }
  12.  
  13.                 infile>>ws;
  14.                 if (isdigit(infile.peek()) || infile.peek() == '-'){
  15.                         infile>>Data.Pka;
  16.                 }
  17.                 DataArray.push_back(Data);                              
  18.          }
Perhaps it has something to do with the way Data is inserted into the array?
Oct 5 '06 #5
r035198x
13,262 MVP
This is the entire loop:

Expand|Select|Wrap|Line Numbers
  1.         while (!infile.eof())
  2.         {
  3.                 infile>>ws;
  4.                 if (isalpha(infile.peek())){
  5.                         infile>>Data.Name;
  6.                 }
  7.  
  8.                 infile>>ws;
  9.                 if (isdigit(infile.peek())){
  10.                         infile>>Data.Index;
  11.                 }
  12.  
  13.                 infile>>ws;
  14.                 if (isdigit(infile.peek()) || infile.peek() == '-'){
  15.                         infile>>Data.Pka;
  16.                 }
  17.                 DataArray.push_back(Data);                              
  18.          }
Perhaps it has something to do with the way Data is inserted into the array?
what makes you sure the last line is being stored twice? How did you test it?
Oct 5 '06 #6
Curten
13 New Member
I tested it by printing the content of the array with this function:

Expand|Select|Wrap|Line Numbers
  1. void displayData (vector<proPkaData> DataArray)
  2. {
  3.         cout << "The values stored in the struct are: " << endl;
  4.         cout << "Name   Index   pKa  " << endl;
  5.         for (int i=0; i<DataArray.size(); i++){
  6.                 cout << DataArray[i].Name << "  ";
  7.                 cout << DataArray[i].Index << "  ";
  8.                 cout << DataArray[i].Pka << "  " << endl;;
  9.         }
  10. }
The call to this function in the main program is just after the loop I posted, where the DataArray is created. All the code used in this small program is now posted... =)
Oct 5 '06 #7
r035198x
13,262 MVP
This is getting worse!
Alright, just post all of it at one go maybe someone might spot it then
Oct 5 '06 #8
D_C
293 Contributor
Here's my guess. You read the last data, then there is something else, like a carriage return and/or newline character before the end of file character. Then, it reads in that data, then it hits the end of file, however, it's already begun the loop. Then, since EOF causes isAlpha(), isDigit() and comparison to '-' to be false, none of the data is modified. As a result, you push the same data on twice. You could test this hypothesis by initializing the data at the beginning of each iteration of the loop.

First thing to do is check the test file and delete any addtional spaces at the end of it. If there isn't any extra characters, you could offset the reading like this, so it reads the next character at the end of the loop, before checking the EOF condition. Finally, you could only push the data if ws != EOF.
Expand|Select|Wrap|Line Numbers
  1. Read 1st Param
  2. while(infile != EOF)
  3. {
  4.   Test 1st Param
  5.     Update 1st Param
  6.  
  7.   Read 2nd Param
  8.   Test 2nd Param
  9.     Update 2nd Param
  10.  
  11.   Read 3rd Param
  12.   Test 3rd Param
  13.     Update 3rd Param
  14.  
  15.   Push Data;
  16.   Read 1st Param;
  17. }
Oct 5 '06 #9
r035198x
13,262 MVP
Here's my guess. You read the last data, then there is something else, like a carriage return and/or newline character before the end of file character. Then, it reads in that data, then it hits the end of file, however, it's already begun the loop. Then, since EOF causes isAlpha(), isDigit() and comparison to '-' to be false, none of the data is modified. As a result, you push the same data on twice. You could test this hypothesis by initializing the data at the beginning of each iteration of the loop.

First thing to do is check the test file and delete any addtional spaces at the end of it. If there isn't any extra characters, you could offset the reading like this, so it reads the next character at the end of the loop, before checking the EOF condition. Finally, you could only push the data if ws != EOF.
Expand|Select|Wrap|Line Numbers
  1. Read 1st Param
  2. while(infile != EOF)
  3. {
  4.   Test 1st Param
  5.     Update 1st Param
  6.  
  7.   Read 2nd Param
  8.   Test 2nd Param
  9.     Update 2nd Param
  10.  
  11.   Read 3rd Param
  12.   Test 3rd Param
  13.     Update 3rd Param
  14.  
  15.   Push Data;
  16.   Read 1st Param;
  17. }
The only possible explanation. You could also add a test for eof that causes the method to return
Oct 6 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1621
by: tim | last post by:
Hello All I am writing an asp app for generating quotes. What I would like to do is allow the user to edit the quote lines without having to open a new page each line. On the quote I have created a loop which goes through the recordset and populates a sort of data grid. Can you use javascript or something so the user can click an edit icon on the appropriate line item in the grid and edit the line details there instead of having to...
11
6084
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h>
5
2492
by: JonH | last post by:
Ok, I have this dynamically created table in my one of my php forms that shows the names of the people the user has entered into a text field. When they hit add a row displays, showing the name they entered. Also, an image that allows them to delete shows beside the name upon creation. The delete removes the name from the table and pushes the other names to the top. Simple stuff, however, if I delete the last name displayed(the bottom of...
5
8708
by: Sen Haerens | last post by:
I'm using string.split(/^$/m, 2) on a curl output to separate header and body. There’s an empty line between them. ^$ doesn’t seem to work... Example curl output: HTTP/1.1 404 Not Found Date: Wed, 22 Feb 2006 00:01:45 GMT Server: Apache/1.3.33 (Darwin) PHP/5.1.2 mod_perl/1.29 Transfer-Encoding: chunked Content-Type: text/html; charset=iso-8859-1
2
1949
by: gavino | last post by:
http://hashphp.org/pastebin.php?format=plain&pid=8181 the $txt line here is giving me problems along with the two if lines below complaint from log: PHP Notice: Use of undefined constant depend - assumed 'depend' in /var/www/html/billingticket/sliputils.php3 on line 203 PHP Notice: Use of undefined constant problem - assumed 'problem' in /var/www/html/billingticket/sliputils.php3 on line 199
17
2531
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
6
18415
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
4
1784
by: Shug | last post by:
Hi, We're reformatting a lot of our project code using the excellent uncrustify beautifier. However, to gain confidence that it really is only changing whitespace (forget { } issues for just now), we were hoping to do a diff - a textual comparison of the files, ignoring whitespace. However, most diffs we've tried can't handle multi-line whitespace, so
4
9941
by: pnsreee | last post by:
Hi all, I have a string "Post.lang.tmp.txt" and i have to replase the ".txt" with "_large.txt". Please help me regarding this problem. I got it in shell scripting using `echo $string | awk -F"/" '{print $NF}' | sed s/.txt/_valid.txt/` Regards Pnsreee
4
2994
by: neha_chhatre | last post by:
please let me know if am using command line arguments, how to send arguments to a C program if i am working on ubuntu say for example suppose name of my program is prog.c if argc=3 then how to send those three arguments(pt1.txt,pt2.txt,pt3.txt) to prog.c through command prompt if i am working on linux
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
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
10550
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...
1
10295
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
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
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
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3799
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.