C++ 清除cout缓冲区(c+;+;)

C++ 清除cout缓冲区(c+;+;),c++,cout,flush,ostream,C++,Cout,Flush,Ostream,您好,我正在编写一个简短的程序来实现一个shell,我遇到了一个不寻常的问题。由于某些原因,我无法清除std::cout缓冲区。该程序不会打印消息。我知道一个简单的解决方案是切换到std::cerr,但是有没有办法让消息用cout打印? 我尝试过的事情: std::cout.flush() 将任何内容写入标准输出后插入std::endl 在输出流中插入std::flush std::cout.setf(std::ios::unitbuf)这是我发现的应该取消缓冲输出的东西 非常感谢您的帮助,以下

您好,我正在编写一个简短的程序来实现一个shell,我遇到了一个不寻常的问题。由于某些原因,我无法清除std::cout缓冲区。该程序不会打印消息。我知道一个简单的解决方案是切换到std::cerr,但是有没有办法让消息用cout打印? 我尝试过的事情:

  • std::cout.flush()
  • 将任何内容写入标准输出后插入
    std::endl
  • 在输出流中插入
    std::flush
  • std::cout.setf(std::ios::unitbuf)这是我发现的应该取消缓冲输出的东西
  • 非常感谢您的帮助,以下是我的代码:

    int main()
    {
        //Tryed this to unbuffer cout, no luck.
        std::cout.setf(std::ios::unitbuf);
    
        std::string input;
    
        //Print out shell prompt and read in input from keyboard.
        std::cout << "myshell> ";   
        std::getline(std::cin, input);
    
        //**********************************************************************
        //Step 1) Read in string and parse into tokens.
        //**********************************************************************
    
        char * buf = new char[input.length() + 1];
        strcpy(buf, input.c_str());
    
        int index = 0;
        char * command[256]; 
    
        command[index] = std::strtok(buf, " ");    //Get first token.
        std::cout << command[index] << std::endl;
    
        while (command[index] != NULL)
        {
            ++index;
            command[index] = std::strtok(NULL," ");    //Get remaining tokens.
            std::cout << command[index] << std::endl;
        }   
    
        std::cout.flush(); //No luck here either
    
        //HERE IS WHERE MY PROBLEM IS.
        std::cout << index << " items were added to the command array" << std::endl;
    
        delete[] buf;
    
        return 0;   
    }
    
    intmain()
    {
    //试着把这个解开,运气不好。
    std::cout.setf(std::ios::unitbuf);
    std::字符串输入;
    //打印外壳提示并从键盘读取输入。
    
    std::cout问题是,在
    while
    循环的最后一次迭代中,您正在将
    NULL
    发送到
    cout
    ,这会导致UB,在您的情况下,会干扰
    cout
    。在将任何内容发送到
    cout
    之前,请检查
    NULL
    ,您很好:

    if (command[index] != NULL) {
        std::cout << command[index] << std::endl;
    }
    
    if(命令[索引]!=NULL){
    
    std::cout如果您需要知道流发生了什么,请记住它们可以携带状态信息()。以下代码可能有助于跟踪您的错误:

     try {
          std::cout.exceptions(std::cout.failbit);          
     } catch(const std::ios_base::failure& e) {
          std::cerr << "stream error: " << e.what() << std::endl;
          std::cout.clear();
     }
    
     // continue working with cout, because std::cout.clear() removed
     // failbit
    
    这是您的代码的外观:

    #include <cstring>
    #include <string>
    #include <iostream>
    int main()
    {
        //Tryed this to unbuffer cout, no luck.
        std::cout.setf(std::ios::unitbuf);
    
        std::string input;
    
        //Print out shell prompt and read in input from keyboard.
        std::cout << "myshell> ";   
        std::getline(std::cin, input);
    
        //**********************************************************************
        //Step 1) Read in string and parse into tokens.
        //**********************************************************************
    
        char * buf = new char[input.length() + 1];
        strcpy(buf, input.c_str());
    
        int index = 0;
        char * command[256]; 
    
        command[index] = std::strtok(buf, " ");    //Get first token.
        std::cout << command[index] << std::endl;
    
        while (command[index] != NULL)
        {
            ++index;
            command[index] = std::strtok(NULL," ");    //Get remaining tokens.
            std::cout << command[index] << std::endl;
        }
    
        // I added from here...
        if(not std::cout) {
          std::cerr << "cout is messed up... fixing it..." << std::endl;
          std::cout.clear();
        }
        // ... to here.
    
        std::cout.flush(); //No luck here either
    
        //HERE IS WHERE MY PROBLEM IS.
        std::cout << index << " items were added to the command array" << std::endl;
    
        delete[] buf;
    
        return 0;   
    }
    

    您似乎已经尝试了列出的所有内容。您确定发布的示例与实际代码匹配吗?什么环境(操作系统、编译器)顺便说一下,“代码”> CUT//CUT>是纯C++。我把C标记从你的帖子中删除了。我在Geany的Linux环境下编码。我将通过链接查看。我使用的编译器选项是:G++WALL-STD= C++ 0x-C“%F”构建选项:G++WALL-STD= C++ 0x-O“%E”“%F”在执行此操作时,我遇到了一个分段错误11
    #include <cstring>
    #include <string>
    #include <iostream>
    int main()
    {
        //Tryed this to unbuffer cout, no luck.
        std::cout.setf(std::ios::unitbuf);
    
        std::string input;
    
        //Print out shell prompt and read in input from keyboard.
        std::cout << "myshell> ";   
        std::getline(std::cin, input);
    
        //**********************************************************************
        //Step 1) Read in string and parse into tokens.
        //**********************************************************************
    
        char * buf = new char[input.length() + 1];
        strcpy(buf, input.c_str());
    
        int index = 0;
        char * command[256]; 
    
        command[index] = std::strtok(buf, " ");    //Get first token.
        std::cout << command[index] << std::endl;
    
        while (command[index] != NULL)
        {
            ++index;
            command[index] = std::strtok(NULL," ");    //Get remaining tokens.
            std::cout << command[index] << std::endl;
        }
    
        // I added from here...
        if(not std::cout) {
          std::cerr << "cout is messed up... fixing it..." << std::endl;
          std::cout.clear();
        }
        // ... to here.
    
        std::cout.flush(); //No luck here either
    
        //HERE IS WHERE MY PROBLEM IS.
        std::cout << index << " items were added to the command array" << std::endl;
    
        delete[] buf;
    
        return 0;   
    }
    
    $ ./a.out
    myshell> 1 2 3
    1
    2
    3
    cout is messed up... fixing it...
    3 items were added to the command array