By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

ifstream: ignoring last line of file

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

Thanks in advance,
Ridimz
Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
WW
Ridimz wrote:
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.

--
WW aka Attila
Jul 19 '05 #2

P: n/a
"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Ridimz wrote:
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.

--
WW aka Attila


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());
Jul 19 '05 #3

P: n/a
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
Jul 19 '05 #4

P: n/a
WW
Ridimz wrote:
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.


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.

--
WW aka Attila
Jul 19 '05 #5

P: n/a
WW
Ridimz wrote:
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?


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!

--
WW aka Attila
Jul 19 '05 #6

P: n/a
"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
This will also skip the last record if it is incomplete!

--
WW aka Attila

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
Jul 19 '05 #7

P: n/a
WW
Ridimz wrote:
"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
This will also skip the last record if it is incomplete!

--
WW aka Attila

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.

--
WW aka Attila
Jul 19 '05 #8

P: n/a
"WW" <wo***@freemail.hu> wrote...

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.

--
WW aka Attila

You've been very helpful!

Thanks,
Ridimz
Jul 19 '05 #9

P: n/a
Ridimz wrote:

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());


This is a poor way for checking if you've reached the end of the file.

http://www.parashift.com/c++-faq-lit....html#faq-15.5

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10

P: n/a
WW
Ridimz wrote:
You've been very helpful!


My mistake. ;-)

--
WW aka Attila
Jul 19 '05 #11

P: n/a
"Kevin Goodsell" <us*********************@neverbox.com> wrote...
This is a poor way for checking if you've reached the end of the file.

http://www.parashift.com/c++-faq-lit....html#faq-15.5

-Kevin
--


Very informative! Thanks for the link.

Ridimz
Jul 19 '05 #12

P: n/a
Ridimz wrote:
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?


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).

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #13

P: n/a
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" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Ridimz wrote:
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.
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.


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.

--
WW aka Attila


-Ghouse
Jul 19 '05 #14

P: n/a
Mohamed Ghouse wrote:
There can be[...]


Please don't top-post.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #15

P: n/a
WW
Mohamed Ghouse wrote:
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.

--
WW aka Attila
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.