Connecting Tech Pros Worldwide Help | Site Map

Whitespace separating lines using extraction operator on file

Kevin Grigorenko
Guest
 
Posts: n/a
#1: Jul 19 '05
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("whatever.txt", std::ios_base::in);
while(!infile.eof())
{
infile >> line;
std::cout << line << std::endl;
line = "";
}
infile.close();

Thanks for your time,
Kevin Grigorenko


Buster
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Kevin Grigorenko" <kzg110@psu.edu> wrote
[color=blue]
> 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?[/color]

This is covered in the FAQ. Good luck.

Regards,
Buster


Kevin Grigorenko
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Buster" <noone@nowhere.com> wrote in message
news:bk06f7$lat$1@news6.svr.pol.co.uk...[color=blue]
> "Kevin Grigorenko" <kzg110@psu.edu> wrote
>[color=green]
> > Okay, i've got a file open, call it infile. I've got a simple[/color][/color]
while(!eof),[color=blue][color=green]
> > but say the first line of my file is "<?xml version="1.0">", then it[/color][/color]
takes[color=blue][color=green]
> > two iterations to get the string. first 'line' is "<?xml", then 'line'[/color][/color]
is[color=blue][color=green]
> > "version="1.0"", what's happening that I need to change the behavior of[/color][/color]
so[color=blue][color=green]
> > that I can get lines delimitted by the newlines in the file?[/color]
>
> This is covered in the FAQ. Good luck.
>
> Regards,
> Buster
>[/color]

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


Buster
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Whitespace separating lines using extraction operator on file



"Kevin Grigorenko" <kzg110@psu.edu> wrote
[color=blue]
> It's fine to point out laziness[/color]
I didn't say that.
[color=blue]
> but you could have at minimum just given me
> some section to look at.[/color]
OK.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.5
[color=blue]
> Every question on this forum has been answered
> somewhere on the internet at some point in time, but searching is painful
> and tedious.[/color]
So I should do it for you?
[color=blue]
> that's why people ask the same questions over and over again in
> newsgroups - it saves time.[/color]
Their time.
[color=blue]
> 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.[/color]
Obvious, yes. Ridiculous, no.
[color=blue]
> 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.[/color]
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.


Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote in news:bk065l$r78$1@f04n12.cac.psu.edu:
[color=blue]
> 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("whatever.txt", std::ios_base::in);[/color]

Don't use eof(), it only reports true when a get operation actually
encounters the EOF.
[color=blue]
> while(!infile.eof())
> {[/color]

This read's space delimited token's try std::getline.
[color=blue]
> infile >> line;[/color]
[color=blue]
> std::cout << line << std::endl;
> line = "";
> }
> infile.close();
>[/color]

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/
Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:
[color=blue]
>
> It's fine to point out laziness, but you could have at minimum just given me
> some section to look at.[/color]

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
[color=blue]
> 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.[/color]

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.
[color=blue]
> 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.[/color]

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.)
[color=blue]
>
> 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.[/color]

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

std::getline(infile, line);

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

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

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

Kevin Grigorenko
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Buster" <noone@nowhere.com> wrote in message
news:bk07s3$gc5$1@newsg3.svr.pol.co.uk...[color=blue]
>
> "Kevin Grigorenko" <kzg110@psu.edu> wrote
>[color=green]
> > It's fine to point out laziness[/color]
> I didn't say that.
>[color=green]
> > but you could have at minimum just given me
> > some section to look at.[/color]
> OK.
> http://www.parashift.com/c++-faq-lit...t.html#faq-5.5[/color]

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.
[color=blue]
>[color=green]
> > Every question on this forum has been answered
> > somewhere on the internet at some point in time, but searching is[/color][/color]
painful[color=blue][color=green]
> > and tedious.[/color]
> So I should do it for you?
>[color=green]
> > that's why people ask the same questions over and over again in
> > newsgroups - it saves time.[/color]
> Their time.
>[color=green]
> > 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.[/color]
> Obvious, yes. Ridiculous, no.
>[color=green]
> > 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[/color][/color]
me to[color=blue][color=green]
> > a more discrete direction.[/color]
> And just maybe, the next person I help won't be gratuitously rude to me,[/color]
again.[color=blue]
> I don't hold out much hope though.[/color]

Who in this case did you actually "help?" By you saying go the FAQ you
actually think you've helped me?
[color=blue]
>
> Again, good luck.
> Buster.
>
>[/color]

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


Buster Copley
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:[color=blue]
> 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?![/color]
That's really interesting. Do go on.

Jon Bell
Guest
 
Posts: n/a
#9: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


In article <bk065l$r78$1@f04n12.cac.psu.edu>,
Kevin Grigorenko <kzg110@psu.edu> wrote:[color=blue]
>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("whatever.txt", std::ios_base::in);
>while(!infile.eof())
>{
> infile >> line;[/color]

Change the line above to

std::getline (infile, line);
[color=blue]
> std::cout << line << std::endl;
> line = "";
>}
>infile.close();[/color]

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 <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Kevin Grigorenko
Guest
 
Posts: n/a
#10: Jul 19 '05

re: Whitespace separating lines using extraction operator on file



"Rob Williscroft" <rtw@freenet.REMOVE.co.uk> wrote in message
news:Xns93F622A14000ukcoREMOVEfreenetrtw@195.129.1 10.130...[color=blue]
> Kevin Grigorenko wrote in news:bk065l$r78$1@f04n12.cac.psu.edu:
>[color=green]
> > 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("whatever.txt", std::ios_base::in);[/color]
>
> Don't use eof(), it only reports true when a get operation actually
> encounters the EOF.
>[color=green]
> > while(!infile.eof())
> > {[/color]
>
> This read's space delimited token's try std::getline.
>[color=green]
> > infile >> line;[/color]
>[color=green]
> > std::cout << line << std::endl;
> > line = "";
> > }
> > infile.close();
> >[/color]
>
> 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/[/color]

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


Kevin Grigorenko
Guest
 
Posts: n/a
#11: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Buster Copley" <buster@none.com> wrote in message
news:bk091k$gt9$2@newsg3.svr.pol.co.uk...[color=blue]
> Kevin Grigorenko wrote:[color=green]
> > It is also quite interesting that whenever someone like myself makes[/color][/color]
these[color=blue][color=green]
> > kinds of comments, people like yourself always reply as you have above,[/color][/color]
even[color=blue][color=green]
> > though the reason they might have given a response as the first one that[/color][/color]
you[color=blue][color=green]
> > gave is to "improve the signal-to-noise ratio." This sure looks like a[/color][/color]
lot[color=blue][color=green]
> > of noise to me?![/color]
> That's really interesting. Do go on.
>[/color]

All I'm saying is that I disagree with "Please don't give them the location
of the appropriate FAQ." If you had just given me where to start in the FAQ
I would have not written the reply that I wrote. Point people to where they
need to go OR the GENERAL direction and then they will figure the rest out.
To point them nowhere as you did is what I consider noise on the board.

Kevin Grigorenko


Kevin Goodsell
Guest
 
Posts: n/a
#12: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:
<snip>

You seem to have gotten your answer. Do you really feel it's wise to now
engage in a flame war with another member of the group who did nothing
out of line? Tempting regulars to killfile you is usually not a good
idea, if you hope for the group to be a useful resource.

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

Buster Copley
Guest
 
Posts: n/a
#13: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:[color=blue]
> All I'm saying is that I disagree with "Please don't give them the location
> of the appropriate FAQ." If you had just given me where to start in the FAQ
> I would have not written the reply that I wrote. Point people to where they
> need to go OR the GENERAL direction and then they will figure the rest out.
> To point them nowhere as you did is what I consider noise on the board.[/color]
Yes, I quite see your point. And you can be quite charming, when you put
your mind to it. I'll certainly consider what you have said.

Regards,
Buster.

Kevin Grigorenko
Guest
 
Posts: n/a
#14: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


[...]
[color=blue]
> 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.)
>[/color]

I actually did, I went there and found nothing blatantly related to my
question, so I posted it here.

[...]
[color=blue]
> I can't actually find the exact entry right now. What you want is[/color]

[...]

It's funny you mention that, I can't either! I did a search for getline on
the FAQ, and it's not there.

Buster Copley, can you please point me to where the use of getline can be
found in the FAQ (this is not meant to by sarcastic in anyway, and if Buster
does, then good for him, but if he doesn't then that means his baseline for
his complete argument is completely false).

Thanks,
Kevin Grigorenko


Kevin Grigorenko
Guest
 
Posts: n/a
#15: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:oWN8b.2510$BS5.1543@newsread4.news.pas.earthl ink.net...[color=blue]
> Kevin Grigorenko wrote:
> <snip>
>
> You seem to have gotten your answer. Do you really feel it's wise to now
> engage in a flame war with another member of the group who did nothing
> out of line? Tempting regulars to killfile you is usually not a good
> idea, if you hope for the group to be a useful resource.
>
> -Kevin
> --
> My email address is valid, but changes periodically.
> To contact me please use the address from a recent posting.
>[/color]

Listen, I'm sorry, I tried to be nice during this whole thing and provide
logical arguments. The simple fact is that I find some regular posters here
to be so audacious with some of their posts, it helps nobody. I can't even
find the FAQ that buster alluded to. Why can't we just be a community and
help each other. I've wasted just as much of my time as everyone else's
writing all of these posts. I probably just could have done a search on
google and found my answer, but I thought, "Hey, it's a pretty simple
question, i'm sure someone can answer it or point my in the right
direction." I just found Buster's reply to my post to be very
disrespectful, irregardless of the fact that it complied perfectly with the
netiquette as written on the FAQ.

Help each other out!

Kevin Grigorenko


Buster Copley
Guest
 
Posts: n/a
#16: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:[color=blue][color=green]
>>I can't actually find the exact entry right now. What you want is[/color]
> [...]
> It's funny you mention that, I can't either! I did a search for getline on
> the FAQ, and it's not there.
>
> Buster Copley, can you please point me to where the use of getline can be
> found in the FAQ (this is not meant to by sarcastic in anyway, and if Buster
> does, then good for him, but if he doesn't then that means his baseline for
> his complete argument is completely false).[/color]
You're right, I was wrong. But I think if you reread the thread, most of
the argument was down to you. After I misdirected you to the FAQ, mostly
I was just commenting on your attitude problem.

Fire away,
Buster.

Kevin Grigorenko
Guest
 
Posts: n/a
#17: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


"Buster Copley" <buster@none.com> wrote in message
news:bk0bpp$p1o$1@news6.svr.pol.co.uk...[color=blue]
> Kevin Grigorenko wrote:[color=green][color=darkred]
> >>I can't actually find the exact entry right now. What you want is[/color]
> > [...]
> > It's funny you mention that, I can't either! I did a search for getline[/color][/color]
on[color=blue][color=green]
> > the FAQ, and it's not there.
> >
> > Buster Copley, can you please point me to where the use of getline can[/color][/color]
be[color=blue][color=green]
> > found in the FAQ (this is not meant to by sarcastic in anyway, and if[/color][/color]
Buster[color=blue][color=green]
> > does, then good for him, but if he doesn't then that means his baseline[/color][/color]
for[color=blue][color=green]
> > his complete argument is completely false).[/color]
> You're right, I was wrong. But I think if you reread the thread, most of
> the argument was down to you. After I misdirected you to the FAQ, mostly
> I was just commenting on your attitude problem.
>
> Fire away,
> Buster.
>[/color]

Well look, I agree that I was the one doing the firing from the start. And
I don't want to start a fight over this, I would just like to see this
newsgroup a little bit more mechanic. Simple question = simple answer ||
simple redirection. I'm not faulting you for pointing me to a non-existent
FAQ, but if you take the one actually interesting comment that I made
throughout this thread, which is that the netiquette provided on the FAQ
page about "Please don't give them the location
of the appropriate FAQ," then this whole situation would have been avoided.
You would have went to try to find where it is, and if it was there,
depending on your mood you would have either given me the direct answer or
just pointed me to the section (which is ALRIGHT, it's fine to make people
dig a little), and in this case it wasn't there, so you probably would have
just replied with the answer as I'm sure that you knew it.

Anyway, let's just follow Rob Williscroft <rtw@freenet.REMOVE.co.uk>'s
example and be very mechanical about this (he gave the first answer of this
I think without any comments or asides). If I had posted "What is
inheritance?", okay bust on me for noise, but even though the question I
posted was quite simple (I actually realized the answer once I saw it, but
I've been out of C++ for a while, hence the couple of posts today for the
first time in a while), it would have been just as simple to reply with the
answer, that's the only point I'd like to make.

Thank you,
Kevin Grigorenko


Rob Williscroft
Guest
 
Posts: n/a
#18: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote in news:bk095a$nk4$1@f04n12.cac.psu.edu:
[color=blue]
> 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?
>[/color]

Yes IIUC the eof() flag is set when you try to read at the EOF,
so if some other error occurs first eof() shouldn't be set.

Some more info here on stream states:

http://www.dinkumware.com/manuals/reader.aspx?b=p/
&h=ios.html#ios_base::iostate

or http://tinyurl.com/na3o

You'll hit a redirect first just click on the logo to get to the
real page.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Christian Brechbühler
Guest
 
Posts: n/a
#19: Jul 19 '05

re: Whitespace separating lines using extraction operator on file


Kevin Grigorenko wrote:[color=blue]
> std::string line;
> std::ifstream infile("whatever.txt", std::ios_base::in);
> while(!infile.eof())
> {
> infile >> line;
> std::cout << line << std::endl;
> line = "";
> }
> infile.close();[/color]

I see you got a lot of flame for this. I'll try an answer.

Use getline(infile, line) instead of infile >> line. See 20.3.15 in
Stroustrup's C++.

Christian

Closed Thread