清除 cout 缓冲区 (c++)
Clearing the cout buffer (c++)
您好,我正在编写一个小程序来实现 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;
}
问题是您在 while
循环的最后一次迭代中将 NULL
发送到 cout
,这会导致 UB,并且在您的情况下会干扰 cout
。在向 cout
发送任何内容之前检查 NULL
就可以了:
if (command[index] != NULL) {
std::cout << command[index] << std::endl;
}
如果您需要了解您的流发生了什么,请记住它们可以携带状态信息 (the iostate
, which I recommend you read about)。以下代码可能有助于跟踪您的错误:
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
您好,我正在编写一个小程序来实现 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;
}
问题是您在 while
循环的最后一次迭代中将 NULL
发送到 cout
,这会导致 UB,并且在您的情况下会干扰 cout
。在向 cout
发送任何内容之前检查 NULL
就可以了:
if (command[index] != NULL) {
std::cout << command[index] << std::endl;
}
如果您需要了解您的流发生了什么,请记住它们可以携带状态信息 (the iostate
, which I recommend you read about)。以下代码可能有助于跟踪您的错误:
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