When use ifstream, how do I ignore the last line of a file if it doesn't
contain any information?
Thanks in advance,
Ridimz 15 5883
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
"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.firstNa me >> acctRec.lastNam e >> acctRec.balance ;
acctRec.acctID = entry.key();
index.insert(en try);
} while (!acctFile.eof( ));
Sorry I accidentally submitted before I was done. searchID = 0; do { searchID++; entry.recNum = searchID; acctFile >> entry.acctID; entry.recNum = searchID; acctFile >> acctRec.firstNa me >> acctRec.lastNam e >>
acctRec.balance ; acctRec.acctID = entry.key(); index.insert(en try); } 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
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.firstNa me >> acctRec.lastNam e >> acctRec.balance ; acctRec.acctID = entry.key(); index.insert(en try); } 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
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.firstNa me >> acctRec.lastNam e >> acctRec.balance ; acctRec.acctID = entry.key(); index.insert(en try); } 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.firstNa me >>
acctRec.lastNam e >>
acctRec.balance )) break;
entry.recNum = ++searchID;
acctRec.acctID = entry.key();
index.insert(en try);
} while (!acctFile.eof( ));
This will also skip the last record if it is incomplete!
--
WW aka Attila
"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
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
"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
Ridimz wrote: searchID = 0; do { searchID++; entry.recNum = searchID; acctFile >> entry.acctID; entry.recNum = searchID; acctFile >> acctRec.firstNa me >> acctRec.lastNam e >> acctRec.balance ; acctRec.acctID = entry.key(); index.insert(en try); } 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ridimz |
last post by:
when use ifstream, how do I ignore the last line of a file if it doesn't
contain any information?
Thanks in advance,
Ridimz
|
by: Ram Laxman |
last post by:
Iam new bie to C++ programming..
I want to write a program which will read the Comma separated
values(CSV) file column wise.
For example:
In a.txt:
"TicketNumber","Phone","CarNumber"
10,20,30
|
by: csvka |
last post by:
Hello,
I wonder if I could pick your brains. I'm beginning to learn about C++. I
have opened a file in my program and I want to read lines from it. I would
like this to be done in a separate function called readline() because I
would also like to do some processing on the line each time (ignoring
comments and so on).
I have:
|
by: hall |
last post by:
Hi.
I ran across a bug in one of my problems and after spending some time
tracking it down i found that the problem arose in a piece of code that
essentially did this:
-----------
ifstream in("in.txt"); if(!in) {cout << "error\n";}
in.close(); if(!in) {cout << "error\n";}
in.close(); if(!in) {cout << "error\n";}
|
by: Eli Luong |
last post by:
Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful....
| |
by: Curten |
last post by:
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?
|
by: dmurray14 |
last post by:
Hey guys,
I'm a C++ newbie here - I've messed with VB, but I mostly stick to web
languages, so I find C++ to be very confusing at times. Basically, I am
trying to import a text file, but I want to do it word by word. I am
confused as to how to do this. Typically, I would think it would make
sense to try and input the words into strings, but for this application
I need to use character arrays and pointers. So what's the best way to
go...
|
by: Angus |
last post by:
Hello
Here is my code
std::ifstream myfile;
std::string line;
long begin,end;
|
by: Boltar |
last post by:
Hi
I'm using ifstream (which I hardly ever use) to read an ascii text
file (containing numbers delimited by newlines) in a loop using
something like:
ifstream infile("filename")
int value;
infile >value;
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |