|
|
std::getline for string and new line
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: Bad use of stringstream temporary? - comp.lang.c++... basic question is whether the following line of code is legal and good (according to the current standard): std::string ... pTastyTreat) { pTastyTreat = new ... catching std::exception by value - comp.lang.c++.moderated ...It also includes a std::string member variable in ... The New Yahoo! Shopping - with improved ... try { throw std::runtime_error( __LINE_FILE__ ); } catch( std ... cannot write std::map via ostream_iterator? - comp.lang.c++ ...... utility> typedef std::map<std::string,std ... (It is interesting to note that the new type cannot be derived from std ... question - comp.lang.c++.moderated... line 4 ... Object (de)serialization - comp.lang.c++... you don't create triangle, so the line creationMap["triangle"] = new ... Shape* create() { return new T; } private: static std::map<std::string ... Data with "," and field sepeator is ",", How to handle this - comp ...... print INLINE } } }' "$FN" > "$NEW_FN" ... can't find the closing double quote for the string # in str it will consume the next line of input by calling # getline ... Simple parsing text , but not for newbie - comp.lang.awk ...Hi gurus, I am new to awk -- but I have sone Perl ... want to read this file line for line. If I find a ... _Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std ... appending a file to another as a new column - comp.lang.awk ...... group to its corresponding pair in the first, as a new ... or for huge files: nawk 'BEGIN { while ( 0<(getline A ... the main gotchas are: a) they're indexed by strings not ... Howto concatenate variable number of fields in a line - comp.lang ...... hard on the variations in shootfoot-getline ... variable number of fields in a line - comp.lang ... Concatenate string ... Parse Numeric Variable into 2 New Variables ... Some text processing questions - comp.lang.vhdl... find that the mega long command line strings to be ... Enlightened by a new understanding of the LINE type from this thread, I ... std.textio.all procedure read - comp.lang ... GAWK: A fix for "missing file is a fatal error" - comp.lang.awk ...On the other hand, making the new behavior ... profiling code, there is a place in pp_string ... know that some people really detest getline and prefer to use command-line file ... getline (C++ Std Lib) - Microsoft Corporation: Software ...The line delimiter. Return Value. The first function returns getline( _Istr, _Str ... In any case, it returns _Istr. See Also <string> Members | string::getline Sample getline - C++ Reference - cplusplus.com - The C++ Resources Network// getline with strings #include <iostream> #include <string> using namespace std; int main () { string str ... string from istream (function ) istream::getline Get line from ... 7/18/2012 4:56:43 AM
|
|
|
|
|
|
|
|
|