473,786 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whitespace separating lines using extraction operator on file

Okay, i've got a file open, call it infile. I've got a simple while(!eof),
but say the first line of my file is "<?xml version="1.0">" , then it takes
two iterations to get the string. first 'line' is "<?xml", then 'line' is
"version="1.0"" , what's happening that I need to change the behavior of so
that I can get lines delimitted by the newlines in the file?

std::string line;
std::ifstream infile("whateve r.txt", std::ios_base:: in);
while(!infile.e of())
{
infile >> line;
std::cout << line << std::endl;
line = "";
}
infile.close();

Thanks for your time,
Kevin Grigorenko
Jul 19 '05 #1
18 2581
"Kevin Grigorenko" <kz****@psu.edu > wrote
Okay, i've got a file open, call it infile. I've got a simple while(!eof),
but say the first line of my file is "<?xml version="1.0">" , then it takes
two iterations to get the string. first 'line' is "<?xml", then 'line' is
"version="1.0"" , what's happening that I need to change the behavior of so
that I can get lines delimitted by the newlines in the file?


This is covered in the FAQ. Good luck.

Regards,
Buster
Jul 19 '05 #2
"Buster" <no***@nowhere. com> wrote in message
news:bk******** **@news6.svr.po l.co.uk...
"Kevin Grigorenko" <kz****@psu.edu > wrote
Okay, i've got a file open, call it infile. I've got a simple while(!eof), but say the first line of my file is "<?xml version="1.0">" , then it takes two iterations to get the string. first 'line' is "<?xml", then 'line' is "version="1.0"" , what's happening that I need to change the behavior of so that I can get lines delimitted by the newlines in the file?


This is covered in the FAQ. Good luck.

Regards,
Buster


It's fine to point out laziness, but you could have at minimum just given me
some section to look at. Every question on this forum has been answered
somewhere on the internet at some point in time, but searching is painful
and tedious, that's why people ask the same questions over and over again in
newsgroups - it saves time. I don't have the time I once had when I was a
teenager, so I try to elicit the help of the nice people in forums. So
please in the future do not just tell people "That is covered somewhere.
Good luck." That is a pretty ridiculous and obvious answer.

I will look at the FAQ as per your suggestion and hope that someone else
with a little bit more sympathy will reply in that timeframe and point me to
a more discrete direction.

Kevin Grigorenko
Jul 19 '05 #3

"Kevin Grigorenko" <kz****@psu.edu > wrote
It's fine to point out laziness I didn't say that.
but you could have at minimum just given me
some section to look at. OK.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.5
Every question on this forum has been answered
somewhere on the internet at some point in time, but searching is painful
and tedious. So I should do it for you?
that's why people ask the same questions over and over again in
newsgroups - it saves time. Their time.
I don't have the time I once had when I was a
teenager, so I try to elicit the help of the nice people in forums. So
please in the future do not just tell people "That is covered somewhere.
Good luck." That is a pretty ridiculous and obvious answer. Obvious, yes. Ridiculous, no.
I will look at the FAQ as per your suggestion and hope that someone else
with a little bit more sympathy will reply in that timeframe and point me to
a more discrete direction.

And just maybe, the next person I help won't be gratuitously rude to me, again.
I don't hold out much hope though.

Again, good luck.
Buster.
Jul 19 '05 #4
Kevin Grigorenko wrote in news:bk******** **@f04n12.cac.p su.edu:
Okay, i've got a file open, call it infile. I've got a simple
while(!eof), but say the first line of my file is "<?xml
version="1.0">" , then it takes two iterations to get the string.
first 'line' is "<?xml", then 'line' is "version="1.0"" , what's
happening that I need to change the behavior of so that I can get
lines delimitted by the newlines in the file?

std::string line;
std::ifstream infile("whateve r.txt", std::ios_base:: in);
Don't use eof(), it only reports true when a get operation actually
encounters the EOF.
while(!infile.e of())
{
This read's space delimited token's try std::getline.
infile >> line; std::cout << line << std::endl;
line = "";
}
infile.close();


while ( std::getline( infile, line ) )
{
std::cout << line << std::endl;
}

This loop will terminate on any stream error.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #5
Kevin Grigorenko wrote:

It's fine to point out laziness, but you could have at minimum just given me
some section to look at.
There is a section specifically about I/O. You could start there.

Here's something in particular you should look at:

http://www.parashift.com/c++-faq-lit....html#faq-15.5
Every question on this forum has been answered
somewhere on the internet at some point in time, but searching is painful
and tedious, that's why people ask the same questions over and over again in
newsgroups - it saves time.
No, it wastes time. You can often get an answer more quickly from the
FAQ than from the group. Besides that, what about the time of the
hundreds of people reading the group? The FAQ is there to save everyone
time.
I don't have the time I once had when I was a
teenager, so I try to elicit the help of the nice people in forums. So
please in the future do not just tell people "That is covered somewhere.
Good luck." That is a pretty ridiculous and obvious answer.
Please in the future read the FAQ before posting. That's basic
Netiquette. (Honestly, I don't expect people to read the whole thing...
it's quite long. But you should at least try to locate your answer there
first.)

I will look at the FAQ as per your suggestion and hope that someone else
with a little bit more sympathy will reply in that timeframe and point me to
a more discrete direction.


I can't actually find the exact entry right now. What you want is

std::getline(in file, line);

But you should rewrite your loop like this (as the FAQ entry I linked
above indicates):

while (std::getline(i nfile, line))
{
// ...
}

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

Jul 19 '05 #6
"Buster" <no***@nowhere. com> wrote in message
news:bk******** **@newsg3.svr.p ol.co.uk...

"Kevin Grigorenko" <kz****@psu.edu > wrote
It's fine to point out laziness I didn't say that.
but you could have at minimum just given me
some section to look at.

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


And because Marshall Cline has written so, it must be true. "It's the old
give-them-a-fish vs. teach-them-to-fish problem." - My question is neither
extremely obvious/trivial, i.e. "What is a class?", nor is it something that
anyone could possibly say is something that needs in-depth study ..... it
just needs an answer to keep my project rolling - What's so hard about just
giving the answer sometimes? If you knew it was in the FAQ, then you
probably know that answer just as much.
Every question on this forum has been answered
somewhere on the internet at some point in time, but searching is painful and tedious. So I should do it for you?
that's why people ask the same questions over and over again in
newsgroups - it saves time.

Their time.
I don't have the time I once had when I was a
teenager, so I try to elicit the help of the nice people in forums. So
please in the future do not just tell people "That is covered somewhere.
Good luck." That is a pretty ridiculous and obvious answer.

Obvious, yes. Ridiculous, no.
I will look at the FAQ as per your suggestion and hope that someone else
with a little bit more sympathy will reply in that timeframe and point me to a more discrete direction.

And just maybe, the next person I help won't be gratuitously rude to me,

again. I don't hold out much hope though.
Who in this case did you actually "help?" By you saying go the FAQ you
actually think you've helped me?

Again, good luck.
Buster.


It is also quite interesting that whenever someone like myself makes these
kinds of comments, people like yourself always reply as you have above, even
though the reason they might have given a response as the first one that you
gave is to "improve the signal-to-noise ratio." This sure looks like a lot
of noise to me?!

If you had just replied with the answer in a concise fassion, this thread
would have been long out of anyone's mind.

Ridiculous... yes.

Kevin Grigorenko
Jul 19 '05 #7
Kevin Grigorenko wrote:
It is also quite interesting that whenever someone like myself makes these
kinds of comments, people like yourself always reply as you have above, even
though the reason they might have given a response as the first one that you
gave is to "improve the signal-to-noise ratio." This sure looks like a lot
of noise to me?!

That's really interesting. Do go on.

Jul 19 '05 #8
In article <bk**********@f 04n12.cac.psu.e du>,
Kevin Grigorenko <kz****@psu.edu > wrote:
Okay, i've got a file open, call it infile. I've got a simple while(!eof),
but say the first line of my file is "<?xml version="1.0">" , then it takes
two iterations to get the string. first 'line' is "<?xml", then 'line' is
"version="1.0" ", what's happening that I need to change the behavior of so
that I can get lines delimitted by the newlines in the file?

std::string line;
std::ifstrea m infile("whateve r.txt", std::ios_base:: in);
while(!infile. eof())
{
infile >> line;
Change the line above to

std::getline (infile, line);
std::cout << line << std::endl;
line = "";
}
infile.close() ;


Also, testing eof() directly in a while loop is almost never correct.
It becomes true only after you have tried and failed to read past the end
of file. In your loop, after you've read the last line, eof() will still
be false, so you go around the loop one more time and try to read again.
This makes eof() true, finally, but it also "reads" an extra garbage line.

It's better to test the input expression directly as the loop condition.
In a boolean context, it evaluates as true if the input succeeded, and
false if it failed:

while (std::getline (infile, line))
{
std::cout << line << std::endl;
}

There's no need to clear the line after you output it, because getline()
will clear the existing contents anyway.

--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #9

"Rob Williscroft" <rt*@freenet.RE MOVE.co.uk> wrote in message
news:Xn******** *************** **********@195. 129.110.130...
Kevin Grigorenko wrote in news:bk******** **@f04n12.cac.p su.edu:
Okay, i've got a file open, call it infile. I've got a simple
while(!eof), but say the first line of my file is "<?xml
version="1.0">" , then it takes two iterations to get the string.
first 'line' is "<?xml", then 'line' is "version="1.0"" , what's
happening that I need to change the behavior of so that I can get
lines delimitted by the newlines in the file?

std::string line;
std::ifstream infile("whateve r.txt", std::ios_base:: in);


Don't use eof(), it only reports true when a get operation actually
encounters the EOF.
while(!infile.e of())
{


This read's space delimited token's try std::getline.
infile >> line;

std::cout << line << std::endl;
line = "";
}
infile.close();


while ( std::getline( infile, line ) )
{
std::cout << line << std::endl;
}

This loop will terminate on any stream error.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Thanks. Following the loop, how would I know if there was an error or if
the loop exitted simply because of completion? Would checking infile.eof()
tell me whether it got to the end or not, and if infile.eof() is false, can
I assume there was some kind of stream error?

Thanks,
Kevin Grigorenko
Jul 19 '05 #10

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

Similar topics

16
2303
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly replicate the input line) The error comes out in correspondance of a triple string.
9
3658
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
1
1327
by: yeutim | last post by:
I can't find anything wrong this with this class. Especially, Overloading Stream-Extraction Operator (in blue). Any one know what do I need to fix this problem please reply. Thank you for your help! --------------------------------------------- class MyData{
0
1233
by: marfi95 | last post by:
I have an xml file that I want to maintain the whitespace on and spacing between my sections. I have something like this. <!-- Comment lines A --> <!-- Comment lines A --> <A> <B>test</B> <B>test1</B> <B>test2</B> </A>
1
2167
by: KevinGPO | last post by:
I am wondering about what's the best and easiest way to strip trailing whitespace from every single file in a folder, recursively. I want to write a program/script so that you pass in a folder name and it'll recursively go through all sub-folders and strips trailing whitespace from every single file. Trailing whitespace includes: 1. whitespaces at end of text lines, strip whitespace but retain text
1
2693
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character block 123.8 123.8 1234.567 12345 12345 1234.567
16
4351
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include <string>
6
2648
by: kaens | last post by:
Hey everyone, this may be a stupid question, but I noticed the following and as I'm pretty new to using xml and python, I was wondering if I could get an explanation. Let's say I write a simple xml parser, for an xml file that just loads the content of each tag into a dict (the xml file doesn't have multiple hierarchies in it, it's flat other than the parent node) so we have <parent>
5
2130
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "cin.get() >>myVarOne >myVarTwo; // illegal The return value of (cin.get() >myVarOne) is an integer, not an iostream object." (http://newdata.box.sk/bx/c/htm/ch16.htm)
0
9647
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
9492
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
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
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...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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
6744
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
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.