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
contain any information?
Thanks in advance,
Ridimz
Ridimz said:When use ifstream, how do I ignore the last line of a file if it
doesn't contain any information?
WW said:Post code. The "normal" extraction operator *will* ignore it.
If there is a blank line following last line of information, it processessearchID = 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());
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());
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?
That did the trick! Thank you very much.This will also skip the last record if it is incomplete!
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've been very helpful!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.
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());
Ridimz said:You've been very helpful!
Kevin Goodsell said:This is a poor way for checking if you've reached the end of the file.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
-Kevin
--
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?
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.
Mohamed said:There can be[...]
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.
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.