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?
12 14867
hmm...it should work, can you post the text file?
Sure, the txt-file looks like this: - GLU 4 3.77
-
GLU 17 3.67
-
GLU 13 3.84
-
GLU 21 3.32
-
C- 30 2.74
-
HIS 5 7.42
-
HIS 10 7.23
-
CYS 6 99.99
-
CYS 7 99.99
-
CYS 11 99.99
-
CYS 20 99.99
-
CYS 7 99.99
-
CYS 19 99.99
-
TYR 14 10.07
-
TYR 19 9.92
-
TYR 16 10.07
-
TYR 26 10.07
-
N+ 1 7.86
-
LYS 29 9.73
-
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...
Sure, the txt-file looks like this: - GLU 4 3.77
-
GLU 17 3.67
-
GLU 13 3.84
-
GLU 21 3.32
-
C- 30 2.74
-
HIS 5 7.42
-
HIS 10 7.23
-
CYS 6 99.99
-
CYS 7 99.99
-
CYS 11 99.99
-
CYS 20 99.99
-
CYS 7 99.99
-
CYS 19 99.99
-
TYR 14 10.07
-
TYR 19 9.92
-
TYR 16 10.07
-
TYR 26 10.07
-
N+ 1 7.86
-
LYS 29 9.73
-
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.
This is the entire loop: - while (!infile.eof())
-
{
-
infile>>ws;
-
if (isalpha(infile.peek())){
-
infile>>Data.Name;
-
}
-
-
infile>>ws;
-
if (isdigit(infile.peek())){
-
infile>>Data.Index;
-
}
-
-
infile>>ws;
-
if (isdigit(infile.peek()) || infile.peek() == '-'){
-
infile>>Data.Pka;
-
}
-
DataArray.push_back(Data);
-
}
Perhaps it has something to do with the way Data is inserted into the array?
This is the entire loop: - while (!infile.eof())
-
{
-
infile>>ws;
-
if (isalpha(infile.peek())){
-
infile>>Data.Name;
-
}
-
-
infile>>ws;
-
if (isdigit(infile.peek())){
-
infile>>Data.Index;
-
}
-
-
infile>>ws;
-
if (isdigit(infile.peek()) || infile.peek() == '-'){
-
infile>>Data.Pka;
-
}
-
DataArray.push_back(Data);
-
}
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?
I tested it by printing the content of the array with this function: - void displayData (vector<proPkaData> DataArray)
-
{
-
cout << "The values stored in the struct are: " << endl;
-
cout << "Name Index pKa " << endl;
-
for (int i=0; i<DataArray.size(); i++){
-
cout << DataArray[i].Name << " ";
-
cout << DataArray[i].Index << " ";
-
cout << DataArray[i].Pka << " " << endl;;
-
}
-
}
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... =)
This is getting worse!
Alright, just post all of it at one go maybe someone might spot it then
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. - Read 1st Param
-
while(infile != EOF)
-
{
-
Test 1st Param
-
Update 1st Param
-
-
Read 2nd Param
-
Test 2nd Param
-
Update 2nd Param
-
-
Read 3rd Param
-
Test 3rd Param
-
Update 3rd Param
-
-
Push Data;
-
Read 1st Param;
-
}
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. - Read 1st Param
-
while(infile != EOF)
-
{
-
Test 1st Param
-
Update 1st Param
-
-
Read 2nd Param
-
Test 2nd Param
-
Update 2nd Param
-
-
Read 3rd Param
-
Test 3rd Param
-
Update 3rd Param
-
-
Push Data;
-
Read 1st Param;
-
}
The only possible explanation. You could also add a test for eof that causes the method to return
Adding "infile>>ws;" at the end of the loop solved the problem... =)
Adding "infile>>ws;" at the end of the loop solved the problem... =)
-
while (infile>>ws)
-
{
-
if (isalpha(infile.peek())){
-
infile>>Data.Name;
-
}
-
if (isdigit(infile.peek())){
-
infile>>Data.Index;
-
}
-
if (isdigit(infile.peek()) || infile.peek() == '-'){
-
infile>>Data.Pka;
-
}
-
DataArray.push_back(Data);
-
}
-
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
at the end of the loop, add this line:
inFile.ignore(1);
It should take away the rewritten line of text
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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>...
|
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...
|
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...
|
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...
|
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,
|
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'
|
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...
|
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 |...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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: 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...
|
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...
| |