std::getline for string and new line

  • Follow


I am using getline(stream, string) to read lines from a text file.
How do I tell if there is a newline for the line just read?

John.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply John 8/4/2006 1:20:58 AM

In article <eatt5q$811$1@lust.ihug.co.nz>, John Grabner
<grabnerj@ihug.co.nz> wrote:

> I am using getline(stream, string) to read lines from a text file.
> How do I tell if there is a newline for the line just read?
>   
  It extracts chars from the stream and places them in the string untli
one of the following occurs
   it finds a '\n'  [does not modify stream]
   if attempts to read past the stream [end of file] [sets
stream.eofbit,might also set failbit not sure]
   it the string will grow beyond its max_size[ sets stream.fail_bit]

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Carl 8/4/2006 2:37:00 AM


John Grabner wrote:
> I am using getline(stream, string) to read lines from a text file.
> How do I tell if there is a newline for the line just read?

One way to find this out is to read char by char, IOW not to use
std::getline.


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Maxim 8/4/2006 12:03:21 PM

Maxim Yegorushkin wrote:
> John Grabner wrote:
>> I am using getline(stream, string) to read lines from a text
>> file.  How do I tell if there is a newline for the line just
>> read?

> One way to find this out is to read char by char, IOW not to
> use std::getline.

You don't have to read every character char by char.  Something
like:

     while ( source.peek() != EOF ) {
         std::string         line ;
         if ( ! std::getline( source, line ) ) {
             //  Non-terminated line...
         }
         //  ...
     }

should work, I think.

-- 
James Kanze                                    kanze.james@neuf.fr
Conseils en informatique orient�e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France +33 (0)1 30 23 00 34

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply James 8/15/2006 3:55:54 PM

3 Replies
212 Views

(page loaded in 0.07 seconds)

Similiar Articles:













7/18/2012 4:56:43 AM


Reply: