Help using pipes

I'm trying to use pipes to write some data on a program stdin and get the stdout.

I found this one on the internet and changed it, (the original didn't seems to work to):

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#define WRITE 1
#define READ 0

using namespace std;

int main()
{
char buf[5];
int fd_in[2], fd_out[2],
c;
pipe(fd_in);pipe(fd_out);
if(fork() == 0)
{
close(fd_in[WRITE]);dup2(fd_in[READ], READ);
close(fd_out[READ]); dup2(fd_out[WRITE], WRITE);

dup2(fd_out[WRITE], WRITE); //stdout -> pipe's write (everything what would go to stdout will go to the pipe's write...
execlp("sort", "sort", NULL);
}

write(fd_out[1], "c\n", 2);
write(fd_out[1], "a\n", 2);
write(fd_out[1], "b\n", 2);

while((c = read(fd_in[0], buf, 5)) > 0)
write(1, buf, c);

return 0;
}


I need to write some data on the sort's stdin and get the stdout, someone can help me?
I forgot a lot of C and POSIX.
But I noticed you don't have a parent process branch for the fork().
On a minor note, you don't do perror() checking, which would help you.

As far as your post is concerned, please use [code]int a=5; // code [/code] tags when copy-pasting code.

Maybe this site will help you now, or serve as a reference:
http://www.cs.cf.ac.uk/Dave/C/node23.html#SECTION002300000000000000000
Sorry, it was my seccond post, so i didn't noticed the code tag.
I'm calling like

echo "text" | myprogram...

and myprogram calls another one and send my stdin as stdin to the program and get the stdout...

If i want to do something like

cat file.txt | ./myprogram > tarfile.zip i should be able to do that, because my program needs to redirect the call to the output.

In this example i'll get the cat as input and send to the "zip" process, get the output of the zip process and send his output to my output.
Must you use a pipe to get the stdin input?
Because if not you could use fread(a, b, c, stdin); for the input.
Then you use two pipes and dup2(), like you tried.
And finally fwrite(a, b, c, stdout);.

If you need to use write() and read() just remember that 0 = stdin and 1 = stdout.
Just taking a quick glance at your code, you're doing dup2 on the child's stdout twice, I don't know what the effect of this is, but I don't think it's right. Also, you haven't closed either end of either pipe in the parent.
I tried to write on the stdout and read on my stdin.. but i need to get the stdout of the execl program
fixed the duplicated dup2.. but still the same thing
Like I already said, you need to close one end of both pipes in the parent.

http://www.advancedlinuxprogramming.com/

How can i do that... the close before the dup dont work like that?
The close before the dup are in the child, not the parent. I suggest you go and read the book I provided a link to and learn how fork and pipe work.
I just found how to exec more than one process.
I need to start one process, send data to it e receive the returned data from it.

thanks
I can write data to the program, but i don't know how to get the stdout and send to one variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sstream>
#include <iostream>

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)) > 0){
			write(pipe_fd[1], buffer, num_bytes);
			}
		close(pipe_fd[1]);
		wait((int*)getpid());
	}

	return 0;
}
Topic archived. No new replies allowed.