getline() — how many kinds in C/C++ std library

See also https://bintanvictor.wordpress.com/2017/03/17/cparsing-simple-text-experience/

(Note All the getline() functions below are designed to return text from a File or cin — actually a one-way stream. Inapplicable to sockets.)

1) Most documentations mention the older c-str-based istream::getline() http://www.cplusplus.com/reference/istream/istream/getline/. However,

2) http://www.cplusplus.com/reference/string/getline/ is another getline(). Unlike the c-str versions of istream::getline() method, this std::string version is implemented as free function instead of member function.

This version is more modern. C++11 added new features to this free function, but didn’t bother with (1)

I feel cin can emulate it with for(; !cin.eof(); cin>>str3){…}. Of course, cin can also parse numbers!

Between these 2 getline() functions,
– One uses c-str; the other uses c++ string class.
– One is a member function; the other a free function

Similarities —
– both require streams, therefore unusable in C.

http://www.learncpp.com/cpp-tutorial/132-input-with-istream/ discusses both.

There’s an IKM question on the member function (1).

3) http://www.gnu.org/software/libc/manual/html_node/Line-Input.html describes a prehistoric ANSI-C standard library function — not using string Class or istream Class. http://crasseux.com/books/ctutorial/getline.html is a short and sharp tutorial on it.

——- philosophically ——-
We can’t assume the most “everyday” programming tasks in a major language are always well-covered online — completely covered, with the confusing details pointed out. Reading a text file is basic but supported by a confusing bunch of alternatives, all named getline().

http://www.augustcouncil.com/~tgibson/tutorial/iotips.html does describe pitfalls of istream::getline()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s