Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;关不好_C++_Daemon_Cout - Fatal编程技术网

C++ c++;关不好

C++ c++;关不好,c++,daemon,cout,C++,Daemon,Cout,我想编写一个充当远程shell的守护进程。因此,我必须关闭stdin、stdout和stderr 现在我想知道这是怎么做的 std::streambuf * old = std::cout.rdbuf(mStdOut.rdbuf()); std::cout << "Bla" << std::endl; std::string text = mStdOut.str(); std::streambuf*old=std::cout.rdbuf(mStdOut.rdbuf())

我想编写一个充当远程shell的守护进程。因此,我必须关闭stdin、stdout和stderr

现在我想知道这是怎么做的

std::streambuf * old = std::cout.rdbuf(mStdOut.rdbuf());
std::cout << "Bla" << std::endl;
std::string text = mStdOut.str();
std::streambuf*old=std::cout.rdbuf(mStdOut.rdbuf());

std::cout您应该在终止程序之前将原始缓冲区恢复到
cout


您无法删除它,因为您不知道它是否是动态分配的(或者如果是,其他代码是否会调用delete)。

您需要做的就是关闭实际的文件描述符。这当然是特定于平台的,但执事也是如此。因此,对于POSIX平台,您可以使用POSIXAPI来实现这一点。重定向
cout
后,您需要执行以下操作:

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
您可能还想阅读:


虽然这是真的,但这并不能回答问题。答案是不能通过
cin
/
cout
/
cerr
完成,但必须对底层POSIX文件句柄执行。好的,问题分为两部分。我回答了C++和 CUT部分-不要删除流缓冲区。