【发布时间】:2020-06-26 03:54:13
【问题描述】:
我想将终端中执行命令的输出重定向到 C++ 中的字符串。我不希望命令输出出现在终端中,因为我使用的是 ncurses 显示。 有什么办法可以做到吗?
我正在使用 Linux 终端。
我考虑过重定向到临时文件,然后读取文件的内容。但我更不想引入 tmp 文件。 澄清一下,我希望能够做到以下几点:
- 通过 execve 回显“hello world”
- 将终端的输出重定向到一个std::string,这样它实际上不会在这个阶段显示在终端中。
- 在 ncurses 中使用 printw 函数将命令输出打印到 ncurses 窗口
我目前正在寻找使用 termios.h 和可能的 dup2 功能的可能实现
我的代码如下所示:
std::string containerForOutput;
// some method which redicects execve into containerForOutput here
char cmd[100];
strcpy(cmd, "/usr/bin/");
strcat(cmd, argv[0]);
// execute the command using execve, but
// will not output to terminal after command is given
// and will instead store it in the string containerForOutput
int result = execve(cmd, argv, environ);
perror("Fallback Shell");
使用字符串流不起作用。
【问题讨论】:
-
这个
std::string应该存在于哪个进程中? 那个进程和生成数据的进程之间是什么关系?父母和孩子分别?你应该展示你到目前为止编写的代码。 -
在this 发帖。