Whitespace separating lines using extraction operator on file

K

Kevin Grigorenko

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
 
B

Buster

Kevin Grigorenko said:
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
 
K

Kevin Grigorenko

Buster said:
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
 
B

Buster

Kevin Grigorenko said:
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-lite/how-to-post.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.
 
J

Jon Bell

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;

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.
 
R

Rob Williscroft

Kevin Grigorenko wrote in
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);

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

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.
 
K

Kevin Grigorenko

Buster said:

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.
So I should do it for you?

Their time.

Obvious, yes. Ridiculous, no.

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
 
K

Kevin Goodsell

Kevin said:
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-lite/input-output.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(infile, line);

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

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

-Kevin
 
B

Buster Copley

Kevin said:
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.
 
K

Kevin Grigorenko

Rob Williscroft said:
Kevin Grigorenko wrote in

Don't use eof(), it only reports true when a get operation actually
encounters the EOF.


This read's space delimited token's try std::getline.



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

This loop will terminate on any stream error.

HTH

Rob.

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
 
K

Kevin Grigorenko

Buster Copley said:
That's really interesting. Do go on.

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
 
K

Kevin Goodsell

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
 
B

Buster Copley

Kevin said:
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.
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.
 
K

Kevin Grigorenko

[...]
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 actually did, I went there and found nothing blatantly related to my
question, so I posted it here.

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

[...]

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
 
K

Kevin Grigorenko

Kevin Goodsell said:
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

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
 
B

Buster Copley

Kevin said:
I can't actually find the exact entry right now. What you want is
[...]
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).
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.
 
K

Kevin Grigorenko

Buster Copley said:
Kevin said:
I can't actually find the exact entry right now. What you want is
[...]
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).
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.

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 <[email protected]>'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
 
R

Rob Williscroft

Kevin Grigorenko wrote in
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?

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.
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

Kevin said:
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();

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
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top