468,463 Members | 2,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

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 13850
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

Post your reply

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

Similar topics

1 post views Thread by tim | last post: by
11 posts views Thread by name | last post: by
5 posts views Thread by Sen Haerens | last post: by
2 posts views Thread by gavino | last post: by
6 posts views Thread by Daniel Mark | last post: by
4 posts views Thread by neha_chhatre | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.