I want to redirect the output from executing a command in the terminal to a string in c++. I dont want the command output to appear at all in the terminal because i am using an ncurses display. is there any way i can do this?
i am using a Linux terminal.
I have considered redirecting to a temp file then reading the contents of a file. But i would much prefer not to introduce tmp files. to clarify, i want to be able to do the following:
- echo "hello world" via execve
- redirect the output from the terminal to an std::string, so that it does not in fact show in the terminal at this stage.
- use printw function in ncurses to print the command output to an ncurses window
i am currently looking for possible implementations using features from termios.h and possibly dup2
my code looks like this:
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");
Using a stringstream did not work.