开发者

Why cin.get(char *, int, char t) and cin.getline(char *, int, char t)?

The documentation says the cin.get(...) will leave the termination character(parameter t) in the buffer! I am wondering how could this be useful compared to getline(...) which will discard the termination character... 开发者_JS百科BTW, suppose I have char buf[256], how can I flush that buf to cout?


For lines, the terminating newline is considered part of the line, and you generally want to remove it when reading a line. Convention has determined that the resulting string containing the line not contain that newline, however, which is why getline discards it instead of storing it. (Other languages/libraries don't make the latter decision.)

For things other than lines, the delimiter may not be considered part of the field being extracted, so you want it to be left and later read.

suppose I have char buf[256], how can I flush that buf to cout?

If that's a C-string (terminated by a \0), then cout << buf;. Otherwise you can cout.write it as an unformatted sequence of bytes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜