我的程序(Solaris 10上的C ++)在从shell启动时通过wcout将输出写入其终端。但是当我从Sun Studio中执行它时,或者文件管理器没有终端,并且输出出现在Sun Studio输出窗口中或者根本没有。
我希望在三种情况中的任何一种情况下打开自己的终端窗口并将wcout附加到此终端窗口。我希望这可以通过C ++系统调用来完成程序本身,而不是从某些shell或脚本执行程序的方式。因为然后在Studio IDE中执行并双击文件管理器仍然会产生相同的效果。
作为一名Windows程序员,对我来说似乎很自然,但我无法知道如何在我的Unix书籍和网络中完成这项工作。我要求做错事,真的很难做,还是我错过了什么?
答案 0 :(得分:5)
以下是你想要的。它仍然有一些错误:
也许其他人知道如何修复这些错误(以及我可能没有注意到的任何其他错误)。
#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;
}
答案 1 :(得分:3)
您想要输出到文件(重定向,使用日志记录API或关闭标准输出/重新打开它作为文件)。然后在您选择的终端中使用tail -f
尾随它。
这增加了保存日志输出以供查看的好处,即使终端崩溃/被杀死。
答案 2 :(得分:0)
当您调用程序时,请运行myprog 1 2 3 a b c
,而不是运行xterm -e myprog 1 2 3 a b c
。
答案 3 :(得分:0)
我建议创建一个shell脚本来运行您传递程序以执行的终端,然后您应该从文件管理器调用该脚本而不是您的程序。
您的script.sh:
#!/ bin / sh的
xterm -e / path_to_your_program / your_program
答案 4 :(得分:0)
使用 mknod
在 /tmp 中创建管道每个 linux 都有 /tmp 并且每个人总是允许使用它
system("mknod /tmp/printing_pipe pipe");
system("qterminal -e tail -f /tmp/printing_pipe");
写入 /tmp/printing_pipe
以使用它