Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite data to pipe C++
    primarykey
    data
    text
    <p>i need to do something like</p> <pre><code>echo "data" | cat </code></pre> <p>Using </p> <pre><code>echo "data" | my program </code></pre> <p>And inside my program calls the cat and sends my stdin to the cat stdin and get the stdout from the cat.</p> <p>I already fork the process, close the write and the read, dup2 them and execl.. So i can get the stdout from it, if i do one execl("/bin/sh", "sh", "-c", "ls -lahtr", NULL) i can get the file list as the output.</p> <p>but i don't know how to send data, like send the echo data that i read from my stdin and send to the execl("/bin/sh", "sh", "-c", "cat", NULL) stdin and return my echo string.</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;sstream&gt; #include &lt;iostream&gt; using namespace std; int main() { int ficheiro_fd; int pipe_fd[2]; char buffer[20]; int num_bytes; pipe(pipe_fd); switch ( fork() ) { case -1: exit(1); case 0: close(pipe_fd[1]); dup2(pipe_fd[0], 0); execlp("/usr/bin/base64"," ", NULL); break; default: close(pipe_fd[0]); //ficheiro_fd = open("output.txt", O_RDONLY); while ((num_bytes = read(fileno(stdin), buffer, 1)) &gt; 0){ write(pipe_fd[1], buffer, num_bytes); } close(pipe_fd[1]); wait((int*)getpid()); } return 0; } </code></pre> <p>With this code i can send some data to the program and it writes on the screen, i want to know how i can get the stdout and send to one variable. Thanks for the help ppl.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload