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

Last line of txt-file read twice with ifstream

13
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?
Oct 5 '06 #1
12 14867
hmm...it should work, can you post the text file?
Oct 5 '06 #2
Curten
13
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 8TB
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
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 8TB
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
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 8TB
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 100+
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 8TB
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
Curten
13
Adding "infile>>ws;" at the end of the loop solved the problem... =)
Oct 6 '06 #11
Fac51
1
Adding "infile>>ws;" at the end of the loop solved the problem... =)
Expand|Select|Wrap|Line Numbers
  1.         while (infile>>ws)
  2.         {
  3.                 if (isalpha(infile.peek())){
  4.                         infile>>Data.Name;
  5.                 }
  6.                 if (isdigit(infile.peek())){
  7.                         infile>>Data.Index;
  8.                 }
  9.                 if (isdigit(infile.peek()) || infile.peek() == '-'){
  10.                         infile>>Data.Pka;
  11.                 }
  12.                 DataArray.push_back(Data);                              
  13.          }
  14.  
there's a way as well....
though you already found a way
but when "while(!inFile.eof)" reads the last line twice, it's a "feature", thats what i heard
i think it's gay
Nov 15 '06 #12
at the end of the loop, add this line:

inFile.ignore(1);


It should take away the rewritten line of text
Feb 15 '07 #13

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

Similar topics

1
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...
11
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>...
5
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...
5
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...
2
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...
17
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
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
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...
4
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 |...
4
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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...

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.