C++ cin char read symbol-by-symbol

前端 未结 3 1838
清歌不尽
清歌不尽 2020-12-18 23:28

I need to read symbol-by-symbol. But I don\'t know how to read until end of input. As exemple test system will cin>>somecharvariable m times. I have to read symbol-by-symbol

3条回答
  •  甜味超标
    2020-12-18 23:53

    Try this:

    #include 
    using std::cin;
    using std::cout;
    
    int main(int argc, char *argv[])
    {
        char ch;
        unsigned m = 10;
        while (cin && m--) {
            cin.read(&ch, sizeof(ch));
            cout << ch;
        }
        return 0;
    }
    

提交回复
热议问题