Kit*_*sto 4 c++ unix terminal redirect stdout
我的程序(Solaris 10上的C++)在从shell启动时通过wcout将输出写入其终端.但是,当我在Sun Studio中执行它时,或者文件管理器没有终端,并且输出出现在Sun Studio输出窗口中或根本没有出现.
我想在三种情况中的任何一种情况下打开自己的终端窗口并将wcout附加到此终端窗口.我希望这可以通过C++系统调用来完成程序本身,而不是从某些shell或脚本执行程序的方式.因为然后在Studio IDE中执行并双击文件管理器仍然会产生相同的效果.
作为一个Windows程序员,对我来说似乎很自然,但我无法在我的Unix书籍和网络中找到这样做.我要求错误的事情,这真的很难做,还是我错过了什么?
以下是您想要的.它仍然有一些错误:
也许其他人知道如何修复这些错误(以及我可能没有注意到的任何其他错误).
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
int main()
{
int pt = posix_openpt(O_RDWR);
if (pt == -1)
{
std::cerr << "Could not open pseudo terminal.\n";
return EXIT_FAILURE;
}
char* ptname = ptsname(pt);
if (!ptname)
{
std::cerr << "Could not get pseudo terminal device name.\n";
close(pt);
return EXIT_FAILURE;
}
if (unlockpt(pt) == -1)
{
std::cerr << "Could not get pseudo terminal device name.\n";
close(pt);
return EXIT_FAILURE;
}
std::ostringstream oss;
oss << "xterm -S" << (strrchr(ptname, '/')+1) << "/" << pt << " &";
system(oss.str().c_str());
int xterm_fd = open(ptname,O_RDWR);
char c;
do read(xterm_fd, &c, 1); while (c!='\n');
if (dup2(pt, 1) <0)
{
std::cerr << "Could not redirect standard output.\n";
close(pt);
return EXIT_FAILURE;
}
if (dup2(pt, 2) <0)
{
std::cerr << "Could not redirect standard error output.\n";
close(pt);
return EXIT_FAILURE;
}
std::cout << "This should appear on the xterm." << std::endl;
std::cerr << "So should this.\n";
std::cin.ignore(1);
close(pt);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5779 次 |
| 最近记录: |