在 C++ 中清除空输入缓冲区的安全操作
Safe operation to clear the empty input buffer in C++
我正在看 this post 和其他一些。如果在输入缓冲区已经为空时调用 ignore() 会发生什么?我在下面的代码中观察到,如果在缓冲区已经为空时调用 ignore() ,它将不起作用并等待先输入某个字符。
int main(void)
{
char myStr[50];
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
cout<<"Enter the String\n";
cin>>myStr;
// After reading remove unwanted characters from the buffer
// so that next read is not affected
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
}
cin.clear() after ignore() 如果缓冲区看起来已经是空的,则会产生进一步的问题。我想在 cin() 之后清除缓冲区是安全的。但是,如果我不知道输入缓冲区的状态并且即使它已经为空我也清除了怎么办?我是否必须先使用 cin.fail() 或类似的东西检查输入缓冲区是否为空?
其次,cin 本身可能不安全,因为 space 是不允许的。因此 getline() 是由一些 SO post 给出的 here 建议的。但是 getline() 是否也需要清除输入缓冲区或者它总是安全的?下面的代码是否可以正常工作(现在可以工作,但现在确定它是否是安全代码)。
void getString(string& str)
{
do
{
cout<<"Enter the String: ";
getline(std::cin,str);
} while (str.empty());
}
其他 SO 参考资料:
Ref 3
分解main
:
int main(void)
{
char myStr[50];
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
一个坏主意,但你已经注意到了。流中必须有换行符,否则您只能坐等。如果用户不期望这种行为,您可能会等待很长时间并且让用户感到沮丧。那场面很糟糕。
cout<<"Enter the String\n";
cin>>myStr;
也是个坏主意,但出于不同的原因。 >>
不知道它应该停在 49 个字符以防止溢出 myStr
。坏事发生在第 50 个字符处。
// After reading remove unwanted characters from the buffer
// so that next read is not affected
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
这个很安全。 >>
不会使用换行符或任何其他空格,并且为了让流从控制台移交数据,必须有人按下回车键并提供换行符。
}
一般的经验法则是不要 ignore
除非你有理由 ignore
,如果你有理由,请立即忽略。不要等到 ignore
的下一个流操作之前,因为如果这个操作是第一个怎么办?还是之前的操作没有给ignore
留下什么?。 ignore
在流中留下您想要的 ignore
d 的操作之后。所以
std::string getfirstword()
{
std::string firstword;
if (std::cin >> firstword)
{
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
return firstword;
}
return ""; // or perhaps
// throw std::runtime_error("There is no first word.");
// is more appropriate. Your call.
}
很好,但是
std::string getfirstword()
{
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
std::string firstword;
if (std::cin >> firstword)
{
return firstword;
}
return "";
}
在所有圣洁的人眼中都是一种冒犯。别这样。
至于getline
,它得到一行。所有这些直到文件末尾或行尾,以先到者为准。它还会为您吃掉行尾,因此您不必担心以后会出现杂散的换行符。
如果您只想要部分行,则必须将其分解。其典型用法类似于
std::string line;
if (std::getline(std::cin,line))
{
std::istringstream istr(line);
std::string firstword;
if (istr >> firstword)
{
// do something with firstword
}
else
{
// there is no firstword. Do something else.
}
}
getline
读取包括换行符在内的所有内容。它不再在流中,所以我认为这是安全的。您不必担心线路末端的垃圾。不过,您可能需要担心下一行。
我正在看 this post 和其他一些。如果在输入缓冲区已经为空时调用 ignore() 会发生什么?我在下面的代码中观察到,如果在缓冲区已经为空时调用 ignore() ,它将不起作用并等待先输入某个字符。
int main(void)
{
char myStr[50];
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
cout<<"Enter the String\n";
cin>>myStr;
// After reading remove unwanted characters from the buffer
// so that next read is not affected
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
}
cin.clear() after ignore() 如果缓冲区看起来已经是空的,则会产生进一步的问题。我想在 cin() 之后清除缓冲区是安全的。但是,如果我不知道输入缓冲区的状态并且即使它已经为空我也清除了怎么办?我是否必须先使用 cin.fail() 或类似的东西检查输入缓冲区是否为空?
其次,cin 本身可能不安全,因为 space 是不允许的。因此 getline() 是由一些 SO post 给出的 here 建议的。但是 getline() 是否也需要清除输入缓冲区或者它总是安全的?下面的代码是否可以正常工作(现在可以工作,但现在确定它是否是安全代码)。
void getString(string& str)
{
do
{
cout<<"Enter the String: ";
getline(std::cin,str);
} while (str.empty());
}
其他 SO 参考资料: Ref 3
分解main
:
int main(void)
{
char myStr[50];
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
一个坏主意,但你已经注意到了。流中必须有换行符,否则您只能坐等。如果用户不期望这种行为,您可能会等待很长时间并且让用户感到沮丧。那场面很糟糕。
cout<<"Enter the String\n";
cin>>myStr;
也是个坏主意,但出于不同的原因。 >>
不知道它应该停在 49 个字符以防止溢出 myStr
。坏事发生在第 50 个字符处。
// After reading remove unwanted characters from the buffer
// so that next read is not affected
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
这个很安全。 >>
不会使用换行符或任何其他空格,并且为了让流从控制台移交数据,必须有人按下回车键并提供换行符。
}
一般的经验法则是不要 ignore
除非你有理由 ignore
,如果你有理由,请立即忽略。不要等到 ignore
的下一个流操作之前,因为如果这个操作是第一个怎么办?还是之前的操作没有给ignore
留下什么?。 ignore
在流中留下您想要的 ignore
d 的操作之后。所以
std::string getfirstword()
{
std::string firstword;
if (std::cin >> firstword)
{
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
return firstword;
}
return ""; // or perhaps
// throw std::runtime_error("There is no first word.");
// is more appropriate. Your call.
}
很好,但是
std::string getfirstword()
{
cin.ignore (std::numeric_limits<std::streamsize>::max(),'\n');
std::string firstword;
if (std::cin >> firstword)
{
return firstword;
}
return "";
}
在所有圣洁的人眼中都是一种冒犯。别这样。
至于getline
,它得到一行。所有这些直到文件末尾或行尾,以先到者为准。它还会为您吃掉行尾,因此您不必担心以后会出现杂散的换行符。
如果您只想要部分行,则必须将其分解。其典型用法类似于
std::string line;
if (std::getline(std::cin,line))
{
std::istringstream istr(line);
std::string firstword;
if (istr >> firstword)
{
// do something with firstword
}
else
{
// there is no firstword. Do something else.
}
}
getline
读取包括换行符在内的所有内容。它不再在流中,所以我认为这是安全的。您不必担心线路末端的垃圾。不过,您可能需要担心下一行。