getline(): how many kinds]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 a std::getline(). Unlike the c-str versions of istream::getline() method, this std::string version is implemented as free function instead of member function in istream class.

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!

Differences between these 2 getline() functions:
– One uses c-str; the other uses std::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 (older) 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 std::string Class or std::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()

发表评论

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

WordPress.com 徽标

您正在使用您的 WordPress.com 账号评论。 登出 /  更改 )

Google photo

您正在使用您的 Google 账号评论。 登出 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 登出 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 登出 /  更改 )

Connecting to %s