如何使用符号分析组织无限循环



我需要组织包含符号分析的无限循环。在C语言中我使用了fgets(buf, N, stdin),假设bufbuf[10]。用户可以键入任何长度的字符串,我可以通过分解输入并检查长度为 10 的部分来分析它。如何在不使用 C 库的情况下在 C++ 中实现这一点。对不起我的英语,如果你不明白我的意思

在C++中,您应该std::cin从标准输入读取。

// #include <iostream>
do
{
    char buf[10]{}; // create array of 10 bytes filled with zeros.
    std::cin.read(buf, 10); // read 10 bytes
    // at this point you should check if std::cin.read succeeded.
    // otherwise you will be reading zeros.
    std::streamsize numRead = std::cin.gcount(); // obtain number of read bytes.
    std::cout << numRead << " " << buf << std::endl; // some printing.
}while(std::cin);

最新更新