清除 cout 缓冲区 (c++)

Clearing the cout buffer (c++)

本文关键字:c++ 缓冲区 cout 清除      更新时间:2023-10-16

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

  1. std::cout.flush()
  2. 在写入标准输出的任何内容后插入std::endl
  3. std::flush插入输出流
  4. 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;   
}

问题是您在while循环的最后一次迭代中将NULL发送到cout,这会导致 UB,并且在您的情况下会干扰cout。在向cout发送任何内容之前检查NULL,您没事:

if (command[index] != NULL) {
    std::cout << command[index] << std::endl;
}

如果您需要知道流发生了什么,请记住它们可以携带状态信息(iostate,我建议您阅读)。以下代码可能有助于跟踪错误:

 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

或者,更简单:

if(not std::cout) {
    // address your error (if it is recoverable)
 }

您的代码如下所示:

#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