ifstream/getline

Discussion in 'C++' started by Christopher Benson-Manica, Apr 1, 2004.

  1. 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';
    }

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Leor Zolman Guest

    On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >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


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 1, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Aggro Guest

    Christopher Benson-Manica wrote:

    > 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.
     
    Aggro, Apr 1, 2004
    #3
  4. Leor Zolman <> spoke thus:

    > (I wrote)
    >>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? ;-)


    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 ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #4
  5. Christopher Benson-Manica

    Aggro Guest

    Christopher Benson-Manica wrote:

    > 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.
     
    Aggro, Apr 1, 2004
    #5
  6. Aggro <> spoke thus:

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


    Well, clearly it's a snippet.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #6
  7. Christopher Benson-Manica

    Aggro Guest

    Aggro, Apr 1, 2004
    #7
  8. Aggro <> spoke thus:

    > http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8


    FAQ or no, not every single post is, or needs to be, a complete C++
    program.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #8
  9. Christopher Benson-Manica

    Leor Zolman Guest

    On Thu, 1 Apr 2004 16:05:36 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >Leor Zolman <> spoke thus:
    >
    >> (I wrote)
    >>>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? ;-)

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

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


    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
    >> 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 ;)


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


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 1, 2004
    #9
  10. Leor Zolman <> spoke thus:

    > 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 ;)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #10
  11. Christopher Benson-Manica

    Mike Wahler Guest

    "Christopher Benson-Manica" <> wrote in message
    news:c4hgie$m5r$...
    > Aggro <> spoke thus:
    >
    > > http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    >
    > 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
     
    Mike Wahler, Apr 1, 2004
    #11
  12. Mike Wahler <> spoke thus:

    > 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 :)

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 1, 2004
    #12
  13. Christopher Benson-Manica wrote:

    > 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.
     
    Jacek Dziedzic, Apr 1, 2004
    #13
  14. Jacek Dziedzic wrote:

    > 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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 1, 2004
    #14
  15. Christopher Benson-Manica

    Buster Guest

    Kevin Goodsell wrote:
    > Jacek Dziedzic wrote:
    >
    >> 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.


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

    Regards,
    Buster.
     
    Buster, Apr 1, 2004
    #15
  16. Christopher Benson-Manica

    Leor Zolman Guest

    On Thu, 1 Apr 2004 17:08:54 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >Mike Wahler <> spoke thus:
    >
    >> 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 :)


    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


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, Apr 1, 2004
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    10
    Views:
    10,110
    Buster Copley
    Jul 14, 2003
  2. Joe
    Replies:
    4
    Views:
    7,789
    Chris Theis
    Jan 22, 2004
  3. Jim Phelps
    Replies:
    1
    Views:
    1,760
    Karl Heinz Buchegger
    Jan 21, 2004
  4. tinks
    Replies:
    1
    Views:
    642
  5. Assertor
    Replies:
    2
    Views:
    2,602
    Assertor
    Feb 26, 2006
Loading...

Share This Page