返回信息流程序:
#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
请按任意键继续. . .
问题:第二行输出为什么是一个空字符串?
这是一条镜像帖。来源:北邮人论坛 / cpp / #89502同步于 2015/11/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
关于getline的问题
helloworld00
2015/11/22镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
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就正确了。
第二条读的不应该是缓存区的残留内容吗?为什么是个空字符串
【 在 Mi 的大作中提到: 】
: std::istream::getline
: istream& getline (char* s, streamsize n );
: istream& getline (char* s, stre
: .........
发自「贵邮」