ifstream/getline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    ifstream/getline

    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)cybers pace.org | don't, I need to know. Flames welcome.
  • Leor Zolman

    #2
    Re: ifstream/getline

    On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
    <ataru@nospam.c yberspace.org> wrote:
    [color=blue]
    >The dumb-o-meter's pegging out today... What, if anything, is wrong
    >with the following code?
    >
    >std::ifstrea m f( "myfile.txt " );
    >if( !f ) {
    > cerr << "Couldn't open file\n";
    >}
    >while( getline(f,s) ) {
    > cout << s << '\n';
    >}[/color]

    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:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Aggro

      #3
      Re: ifstream/getline

      Christopher Benson-Manica wrote:
      [color=blue]
      > The dumb-o-meter's pegging out today... What, if anything, is wrong
      > with the following code?[/color]

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

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

      return 0;
      }

      Now it should compile.

      Comment

      • Christopher Benson-Manica

        #4
        Re: ifstream/getline

        Leor Zolman <leor@bdsoft.co m> spoke thus:
        [color=blue]
        > (I wrote)[color=green]
        >>std::ifstre am f( "myfile.txt " );
        >>if( !f ) {
        >> cerr << "Couldn't open file\n";
        >>}
        >>while( getline(f,s) ) {
        >> cout << s << '\n';
        >>}[/color][/color]
        [color=blue]
        > Is this an interview question? ;-)[/color]

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

        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?
        [color=blue]
        > I
        > could enumerate all the reasons it doesn't compile,[/color]

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

        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)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • Aggro

          #5
          Re: ifstream/getline

          Christopher Benson-Manica wrote:
          [color=blue]
          > If it isn't supposed to compile, I've got problems, because it
          > compiles cleanly.[/color]

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

          Comment

          • Christopher Benson-Manica

            #6
            Re: ifstream/getline

            Aggro <spammerdream@y ahoo.com> spoke thus:
            [color=blue]
            > Then you really got problems or you didn't post the full code.[/color]

            Well, clearly it's a snippet.

            --
            Christopher Benson-Manica | I *should* know what I'm talking about - if I
            ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

            Comment

            • Aggro

              #7
              Re: ifstream/getline

              Christopher Benson-Manica wrote:
              [color=blue]
              > Well, clearly it's a snippet.[/color]

              Please read:


              Comment

              • Christopher Benson-Manica

                #8
                Re: ifstream/getline

                Aggro <spammerdream@y ahoo.com> spoke thus:
                [color=blue]
                > http://www.parashift.com/c++-faq-lit...t.html#faq-5.8[/color]

                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)cybers pace.org | don't, I need to know. Flames welcome.

                Comment

                • Leor Zolman

                  #9
                  Re: ifstream/getline

                  On Thu, 1 Apr 2004 16:05:36 +0000 (UTC), Christopher Benson-Manica
                  <ataru@nospam.c yberspace.org> wrote:
                  [color=blue]
                  >Leor Zolman <leor@bdsoft.co m> spoke thus:
                  >[color=green]
                  >> (I wrote)[color=darkred]
                  >>>std::ifstrea m f( "myfile.txt " );
                  >>>if( !f ) {
                  >>> cerr << "Couldn't open file\n";
                  >>>}
                  >>>while( getline(f,s) ) {
                  >>> cout << s << '\n';
                  >>>}[/color][/color]
                  >[color=green]
                  >> Is this an interview question? ;-)[/color]
                  >
                  >Nah, just an "I'm dumb today!" question, as I indicated.[/color]

                  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?
                  [color=blue]
                  >[color=green]
                  >> Why don't you tell us why you think there's something wrong with it.[/color]
                  >
                  >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?[/color]

                  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)
                  [color=blue][color=green]
                  >> I
                  >> could enumerate all the reasons it doesn't compile,[/color]
                  >
                  >If it isn't supposed to compile, I've got problems, because it
                  >compiles cleanly.
                  >[color=green]
                  >> but that would just be a waste of both of our time...[/color]
                  >
                  >I'm amazed you're still answering my posts, quite frankly ;)[/color]

                  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:
                  An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                  Comment

                  • Christopher Benson-Manica

                    #10
                    Re: ifstream/getline

                    Leor Zolman <leor@bdsoft.co m> spoke thus:
                    [color=blue]
                    > 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?[/color]

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

                    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.
                    [color=blue]
                    > If you're hoping to earn the "Most Annoying Poster" crown, you haven't got
                    > a prayer ;-)[/color]

                    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)cybers pace.org | don't, I need to know. Flames welcome.

                    Comment

                    • Mike Wahler

                      #11
                      Re: ifstream/getline


                      "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
                      news:c4hgie$m5r $1@chessie.cirr .com...[color=blue]
                      > Aggro <spammerdream@y ahoo.com> spoke thus:
                      >[color=green]
                      > > http://www.parashift.com/c++-faq-lit...t.html#faq-5.8[/color]
                      >
                      > FAQ or no, not every single post is, or needs to be, a complete C++
                      > program.[/color]

                      Complete programs are far more likely to engender
                      useful diagnosis.

                      -Mike


                      Comment

                      • Christopher Benson-Manica

                        #12
                        Re: ifstream/getline

                        Mike Wahler <mkwahler@mkwah ler.net> spoke thus:
                        [color=blue]
                        > Complete programs are far more likely to engender
                        > useful diagnosis.[/color]

                        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)cybers pace.org | don't, I need to know. Flames welcome.

                        Comment

                        • Jacek Dziedzic

                          #13
                          Re: ifstream/getline

                          Christopher Benson-Manica wrote:
                          [color=blue]
                          > 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';
                          > }
                          >[/color]

                          I'd go paranoid and try

                          std::ifstream f( "myfile.txt",st d::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.

                          Comment

                          • Kevin Goodsell

                            #14
                            Re: ifstream/getline

                            Jacek Dziedzic wrote:
                            [color=blue]
                            > b) you have cout/cerr redirected :) or their goodbit is cleared.[/color]

                            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.

                            Comment

                            • Buster

                              #15
                              Re: ifstream/getline

                              Kevin Goodsell wrote:[color=blue]
                              > Jacek Dziedzic wrote:
                              >[color=green]
                              >> b) you have cout/cerr redirected :) or their goodbit is cleared.[/color]
                              >
                              >
                              > In reality there is no goodbit, is there? goodbit is considered "set"
                              > when all the error bits are cleared, unless I'm mistaken.[/color]

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

                              Regards,
                              Buster.

                              Comment

                              Working...