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?
12 14941
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
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. - 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
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 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...
|
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>
|
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...
|
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
|
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
| |
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 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
|
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
|
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
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |