Whitespace separating lines using extraction operator on file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Grigorenko

    Whitespace separating lines using extraction operator on file

    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("whateve r.txt", std::ios_base:: in);
    while(!infile.e of())
    {
    infile >> line;
    std::cout << line << std::endl;
    line = "";
    }
    infile.close();

    Thanks for your time,
    Kevin Grigorenko


  • Buster

    #2
    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


    Comment

    • Kevin Grigorenko

      #3
      Re: Whitespace separating lines using extraction operator on file

      "Buster" <noone@nowhere. com> wrote in message
      news:bk06f7$lat $1@news6.svr.po l.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


      Comment

      • Buster

        #4
        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.

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


        Comment

        • Rob Williscroft

          #5
          Re: Whitespace separating lines using extraction operator on file

          Kevin Grigorenko wrote in news:bk065l$r78 $1@f04n12.cac.p su.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("whateve r.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.e of())
          > {[/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.
          --

          Comment

          • Kevin Goodsell

            #6
            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:


            [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(in file, line);

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

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

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

            Comment

            • Kevin Grigorenko

              #7
              Re: Whitespace separating lines using extraction operator on file

              "Buster" <noone@nowhere. com> wrote in message
              news:bk07s3$gc5 $1@newsg3.svr.p ol.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


              Comment

              • Buster Copley

                #8
                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.

                Comment

                • Jon Bell

                  #9
                  Re: Whitespace separating lines using extraction operator on file

                  In article <bk065l$r78$1@f 04n12.cac.psu.e du>,
                  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::ifstrea m infile("whateve r.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@pres by.edu> Presbyterian College
                  Dept. of Physics and Computer Science Clinton, South Carolina USA

                  Comment

                  • Kevin Grigorenko

                    #10
                    Re: Whitespace separating lines using extraction operator on file


                    "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> wrote in message
                    news:Xns93F622A 14000ukcoREMOVE freenetrtw@195. 129.110.130...[color=blue]
                    > Kevin Grigorenko wrote in news:bk065l$r78 $1@f04n12.cac.p su.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("whateve r.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.e of())
                    > > {[/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


                    Comment

                    • Kevin Grigorenko

                      #11
                      Re: Whitespace separating lines using extraction operator on file

                      "Buster Copley" <buster@none.co m> wrote in message
                      news:bk091k$gt9 $2@newsg3.svr.p ol.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


                      Comment

                      • Kevin Goodsell

                        #12
                        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.

                        Comment

                        • Buster Copley

                          #13
                          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.

                          Comment

                          • Kevin Grigorenko

                            #14
                            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


                            Comment

                            • Kevin Grigorenko

                              #15
                              Re: Whitespace separating lines using extraction operator on file

                              "Kevin Goodsell" <usenet1.spamfr ee.fusion@never box.com> wrote in message
                              news:oWN8b.2510 $BS5.1543@newsr ead4.news.pas.e arthlink.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


                              Comment

                              Working...