getline() 和输入流的问题

issues with getline() and input stream

代码

#include<iostream>
int main()
{
    char s[20];
    char ch;
    
    std::cin.getline(s,10);
    std::cout<<"x"<<s<<"x"<<"\n";
    
    std::cin>>ch;
    std::cout<<"x"<<ch<<"x";
    
    std::cin>>ch;
    std::cout<<"x"<<ch<<"x";
    
    std::cin>>ch;
    std::cout<<"x"<<ch<<"x";
}

输出-1

bonaparte    // I entered these 9 characters as input
xbonapartex
a            //  input
xax
b            // input
xbx
c            // input
xcx

输出-2

bonaparte2    // I entered these 10 characters as input
xbonapartex
x x
x x
x x

与第一个输出相比,我刚刚通过键盘输入了一个额外的字母意味着总共 10 个字符 bonaparte2

据我所知,getline 将提取 9 个字符并添加 '\0' 并存储在字符串 s 中。所以流仍然包含字符 2\n。所以首先 cin>>ch 应该取 2 并打印 x2x.

现在第二个 cin>>ch 应该忽略 \n 因为它是前导空白字符,但是 none 正在发生这种情况。程序不要求从键盘输入。

在 Output-2 中,std::cin.getline(s,10); 失败(因为它无法读取完整的行)。然后它将流设置为失败状态,所有后续提取也将失败(除非您 clear() 流)。

if(not std::cin.getline(s, 10)) { // an empty string is considered ok
    if(std::cin.eof()) {
        std::cout << "end of file / input closed\n";
        return 1;
    }
    if(std::cin.bad()) {
        std::cout << "a non-recoverable error has occurred\n";
        return 1;
    }
    // failbit
    std::cout << "Too long string entered\n";
    std::cin.clear();
}

如果您不希望在读取的前 9 个字符内未找到换行符时将流设置为失败状态,则可以改用以下内容。请注意,由于以下原因,它可能仍会设置 eofbitfailbit

if(not std::cin.get(s, 10)) {   // an empty string sets failbit
    if(std::cin.eof()) {
        std::cout << "end of file / input closed\n";
        return 1;
    }
    if(std::cin.bad()) {
        std::cout << "a non-recoverable error has occurred\n";
        return 1;
    }
    // failbit
    std::cout << "No string entered\n";
    std::cin.clear();
}

std::cin.getline(s,10); 中,由于输入大于 stream_size 提供的值,istream::getline 将流设置为失败状态,并阻止进一步输入。

可以很容易地用 std::cin.clear() 修复。使用 std::string 存储输入并让它处理长度也是一种替代方法。

相关:

  • cin.getline( ) with larger size

  • Why would we call cin.clear() and cin.ignore() after reading input?