473,666 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream: ignoring last line of file

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
15 5883
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
"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( ));
Jul 19 '05 #3
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
Jul 19 '05 #4
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.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
Jul 19 '05 #5
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.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
Jul 19 '05 #6
"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
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
"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
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.

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
646
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
6
3383
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
6
4275
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:
4
3730
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";}
9
2536
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....
12
14913
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?
20
5128
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...
2
2598
by: Angus | last post by:
Hello Here is my code std::ifstream myfile; std::string line; long begin,end;
7
3220
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;
0
8443
marktang
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...
0
8356
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,...
0
8781
jinu1996
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...
0
8639
tracyyun
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...
1
6192
isladogs
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...
0
5663
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();...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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
2
1772
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.