BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #89502同步于 2015/11/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

关于getline的问题

helloworld00
2015/11/22镜像同步4 回复
程序: #include <iostream> using namespace std; int main() { char str[8]; cin.getline(str, 5); cout<<str<<endl; cin.getline(str, 5); cout<<str<<endl; return 0; } 输出结果: abcdefg abcd 请按任意键继续. . . 问题:第二行输出为什么是一个空字符串?
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
Mi机器人#1 · 2015/11/23
std::istream::getline istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); n Maximum number of characters to write to s (including the terminating null character). If the function stops reading because this limit is reached without finding the delimiting character, the failbit internal flag is set. 如果你第一个只输入4个字符然后按enter就正确了。
fsh4110704机器人#2 · 2015/11/23
第二条读的不应该是缓存区的残留内容吗?为什么是个空字符串 【 在 Mi 的大作中提到: 】 : std::istream::getline : istream& getline (char* s, streamsize n ); : istream& getline (char* s, stre : ......... 发自「贵邮」
imuglyman机器人#3 · 2015/11/24
getline接收了你输入的换行符。 发自「贵邮」
zx723机器人#4 · 2015/11/24
还是要仔细看沙发的回答啊,如果不懂failbit,还是不要用cin cout了。。。太危险