ifstream/getline

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}
 
L

Leor Zolman

The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}

Is this an interview question? ;-)
Why don't you tell us why you think there's something wrong with it. I
could enumerate all the reasons it doesn't compile, but that would just be
a waste of both of our time...
-leor
 
A

Aggro

Christopher said:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
std::string s;
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}

return 0;
}

Now it should compile.
 
C

Christopher Benson-Manica

Leor Zolman said:
(I wrote)
Is this an interview question? ;-)

Nah, just an "I'm dumb today!" question, as I indicated.
Why don't you tell us why you think there's something wrong with it.

Because nothing gets printed? I know the file exists, and I know I
can open it. I don't see what I'm doing wrong (obviously *sigh*). I
create an ifstream, see whether it's open and then read from it.
getline(f,s) returns f, and that's true unless end of file has been
reached, right?
I
could enumerate all the reasons it doesn't compile,

If it isn't supposed to compile, I've got problems, because it
compiles cleanly.
but that would just be a waste of both of our time...

I'm amazed you're still answering my posts, quite frankly ;)
 
A

Aggro

Christopher said:
If it isn't supposed to compile, I've got problems, because it
compiles cleanly.

Then you really got problems or you didn't post the full code.
 
L

Leor Zolman

Nah, just an "I'm dumb today!" question, as I indicated.

I know, that's why I had the smiley. I was giving you a bit of grief
because I expected you to know, by now, to provide a more complete code
example and explain specifically what the problem you were having was. If
someone stuck a code fragment in front of you and just said, "What's wrong
with this?", how would /you/ react?
Because nothing gets printed? I know the file exists, and I know I
can open it. I don't see what I'm doing wrong (obviously *sigh*). I
create an ifstream, see whether it's open and then read from it.
getline(f,s) returns f, and that's true unless end of file has been
reached, right?

What are the contents of the file? I actually tried this and it worked for
me. I defined s as a string. (And I found a minor bug in the C++ standard
while looking for whether or not getline as you wrote it would work with s
defined as a char[]... something I wouldn't have needed to check, BTW, if
you'd shown how s was defined)
If it isn't supposed to compile, I've got problems, because it
compiles cleanly.


I'm amazed you're still answering my posts, quite frankly ;)

If you're hoping to earn the "Most Annoying Poster" crown, you haven't got
a prayer ;-)
-leor
 
C

Christopher Benson-Manica

Leor Zolman said:
I know, that's why I had the smiley. I was giving you a bit of grief
because I expected you to know, by now, to provide a more complete code
example and explain specifically what the problem you were having was. If
someone stuck a code fragment in front of you and just said, "What's wrong
with this?", how would /you/ react?

Well, I figured it was simple enough to get away with - I included "if
anything" because I didn't see any reason for it not to work... I
guess I should have explained better. Sorry.
What are the contents of the file? I actually tried this and it worked for
me. I defined s as a string. (And I found a minor bug in the C++ standard
while looking for whether or not getline as you wrote it would work with s
defined as a char[]... something I wouldn't have needed to check, BTW, if
you'd shown how s was defined)

I didn't show a definition for s, did I...? Um, well, I did say I was
feeling stupid today :-< My apologies. s is a std::string, FWIW.
I'm leaning toward blaming my implementation again, actually... oh
well.
If you're hoping to earn the "Most Annoying Poster" crown, you haven't got
a prayer ;-)

Well, that's certainly heartbreaking - I'll have to take the rest of
the day off to mope and brood over that one ;)
 
M

Mike Wahler

Christopher Benson-Manica said:
FAQ or no, not every single post is, or needs to be, a complete C++
program.

Complete programs are far more likely to engender
useful diagnosis.

-Mike
 
C

Christopher Benson-Manica

Mike Wahler said:
Complete programs are far more likely to engender
useful diagnosis.

Agreed, I'm just making the point that some code snippets manage to
get useful commentary in spite of their snippiness (if you will). The
expected diagnosis here, of course, is the same as always - that I'm
dumb :)
 
J

Jacek Dziedzic

Christopher said:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}

I'd go paranoid and try

std::ifstream f( "myfile.txt",std::ios::in);
if( !f ) {
cerr << "Couldn't open file" << endl;
}
while( getline(f,s) ) {
cout << s << endl;
}

if it still doesn't work, then my crystall ball thinks
a) you have a serious flaw in some beyond-the-snippet code,
like a memory problem or, generally, an UB somewhere else.
b) you have cout/cerr redirected :) or their goodbit is cleared.
c) myfile.txt is malformed (like by odd end-of-line translation, etc.)
d) try waiting until April the 2nd and see if it does better :)

if neither, then I'm stumped.

You could try inserting an infinite loop in either if or while
to see if it indeed starts to loop infinitely -- that way you'll
know it's your output that's broken, not the stream stuff. Or the
other way round, of course.

- J.
 
K

Kevin Goodsell

Jacek said:
b) you have cout/cerr redirected :) or their goodbit is cleared.

In reality there is no goodbit, is there? goodbit is considered "set"
when all the error bits are cleared, unless I'm mistaken.

-Kevin
 
B

Buster

Kevin said:
In reality there is no goodbit, is there? goodbit is considered "set"
when all the error bits are cleared, unless I'm mistaken.

Yes, std::ios_base::goodbit is defined to be 0 in the standard, e.g.
at 27.4.2.1.3/2.

Regards,
Buster.
 
L

Leor Zolman

Agreed, I'm just making the point that some code snippets manage to
get useful commentary in spite of their snippiness (if you will). The
expected diagnosis here, of course, is the same as always - that I'm
dumb :)

I think it was Mike who recently made the point "If you think it, it will
be true".

I think I know where you're coming from re. the "snippet" issue, but given
that you've seen already in this very thread how omission of pieces just
makes it more difficult for us to assist you, I'm not sure why you were
giving Mike grief here. Get over it, write some code, and most importantly,
/please/ stop trying to convince yourself and tens of thousands of
newsgroup readers of your inferiority! (I don't know if tens of thousands
is the right number or not, but stop doing it anyway)
-leor
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top