ifstream: ignoring last line of file

R

Ridimz

When use ifstream, how do I ignore the last line of a file if it doesn't
contain any information?

Thanks in advance,
Ridimz
 
W

WW

Ridimz said:
When use ifstream, how do I ignore the last line of a file if it
doesn't contain any information?

Post code. The "normal" extraction operator *will* ignore it.
 
R

Ridimz

WW said:
Post code. The "normal" extraction operator *will* ignore it.

searchID = 0;
do
{
searchID++;
entry.recNum = searchID;
acctFile >> entry.acctID;
entry.recNum = searchID;
acctFile >> acctRec.firstName >> acctRec.lastName >> acctRec.balance;
acctRec.acctID = entry.key();
index.insert(entry);
} while (!acctFile.eof());
 
R

Ridimz

Sorry I accidentally submitted before I was done.
searchID = 0;
do
{
searchID++;
entry.recNum = searchID;
acctFile >> entry.acctID;
entry.recNum = searchID;
acctFile >> acctRec.firstName >> acctRec.lastName >> acctRec.balance;
acctRec.acctID = entry.key();
index.insert(entry);
} while (!acctFile.eof());
If there is a blank line following last line of information, it processes
last line of info a second time.

How do I prevent that?

Thanks
 
W

WW

Ridimz said:
searchID = 0;
do
{
searchID++;
entry.recNum = searchID;
acctFile >> entry.acctID;
entry.recNum = searchID;
acctFile >> acctRec.firstName >> acctRec.lastName >>
acctRec.balance; acctRec.acctID = entry.key();
index.insert(entry);
} while (!acctFile.eof());

As far as I see this will ignore it. Since the extraction operator starts
by skipping whitespaces and an empty line is a whitespace.
 
W

WW

Ridimz said:
Sorry I accidentally submitted before I was done.

If there is a blank line following last line of information, it
processes last line of info a second time.

How do I prevent that?

One example:

searchID = 0;
do {
if (!(acctFile >> entry.acctID)) break;
if (!(acctFile >> acctRec.firstName >>
acctRec.lastName >>
acctRec.balance)) break;
entry.recNum = ++searchID;
acctRec.acctID = entry.key();
index.insert(entry);
} while (!acctFile.eof());

This will also skip the last record if it is incomplete!
 
R

Ridimz

This will also skip the last record if it is incomplete!
That did the trick! Thank you very much.
One last question: Say I just wanted to read the first entry on each line
(acctID), how would I go about doing so?

I currently read in all values on the line: which is unnecessary, because I
only need the acctID.

Thanks again!
Ridimz
 
W

WW

Ridimz said:
That did the trick! Thank you very much.
One last question: Say I just wanted to read the first entry on each
line (acctID), how would I go about doing so?

I currently read in all values on the line: which is unnecessary,
because I only need the acctID.

You cannot, it is not a database table. But you can read the rest into the
same variable and simply not use it for anything. Or you can use getline
with a string, put that string into an istringstream and only read the first
thing out of it.
 
R

Ridimz

WW said:
You cannot, it is not a database table. But you can read the rest into the
same variable and simply not use it for anything. Or you can use getline
with a string, put that string into an istringstream and only read the first
thing out of it.
You've been very helpful!

Thanks,
Ridimz
 
J

Jim Fischer

Ridimz said:
Sorry I accidentally submitted before I was done.



If there is a blank line following last line of information, it processes
last line of info a second time.

How do I prevent that?

With the code shown above, you cannot prevent this situation. The
following loop constructs,

while ( !eof ) ...

or

do ... while ( !eof )

are logically wrong because they introduce two different off-by-one
errors at run time. In other words, depending on the input stream's
content, the program will execute the body of the while loop one too few
times, one too many times, or exactly the right number of times. And
changing the file layout will, of course, change the way the program
behaves (e.g., changing the file's content might change the program's
run time behavior so that the program now executes the loop body one to
few times instead of one too many times, etc.). So a loop construct such
as this is logically wrong.


When performing I/O in C/C++ programs, the sequence of events must
always be this:

1) Attempt the I/O operation(s)

2) Test whether the I/O attempt was successful or not

If you reverse this sequence - (2) first, then (1) - this messes things
up - e.g., you end up with off by one errors and other nasty problems.
So you need to reconstruct your loop so that the I/O attempts are
performed *before* the program evaluates the loop's conditional
expression. For example:

T1 acctID;
T2 firstName;
T3 lastName;
T4 balance;

while ( acctFile >> acctID >> firstName >> lastName >> balance ) {
++searchID;
entry.recNum = searchID;
entry.acctID = acctID;
acctRec.firstName = firstName;
acctRec.lastName = lastName;
acctRec.balance = balance;
acctRec.acctID = entry.key();
index.insert(entry);
}

There is apparently some "black magic" going on in the while() loop's
conditional expression - i.e., some istream class methods are being
invoked implicitly, after the I/O operations occur, to determine whether
the I/O attempts were successful or not. FWIW, the while() loop's
conditional expression is effectively the same as this:

while (
acctFile >> acctID >> firstName >> lastName >> balance,
!acctFile.fail() // Test: "The I/O attempts did not fail"
)
{ ... }

Note the use of the comma operator ',' after the word "balance" (i.e.,
after the input operations) and before the fail() method call. Also note
that the program enters the body of the while() loop if the input
attempts "do not fail" (i.e., if the program successfully read the
expected data items into the specified variables).
 
M

Mohamed Ghouse

There can be a possibility of a core dump (on few Unix flavours) if there is
no data available in the file.
It is a bad idea to peform any operations before fetching the data.

WW said:
As far as I see this will ignore it. Since the extraction operator starts
by skipping whitespaces and an empty line is a whitespace.

Your point is correct. But a problem exists if the last line mentioned by
Ridimz becomes the first line.
i.e No data in the file, but file exists.

-Ghouse
 
W

WW

Mohamed said:
There can be a possibility of a core dump (on few Unix flavours) if
there is no data available in the file.
It is a bad idea to peform any operations before fetching the data.

I don't know what do you mean. If there is no data in the file eof will be
set. If you get a crash, that implementation is not conforming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top