Hi All. I am devloping a program that will spawn a child process that will perform the task of reading characters from a text file one char at a time, and piping the data back to the parent process to be printed to the screen. This is what I have so far.
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>
#include<sstream>
#include<signal.h>
void Child(pid_t Handle)
void Parent(pid_t Handle)
main()
{
pid_t pid;
int fd[2];
pipe(fd);
pid = fork();
if(pid == 0)
{
close(fd[0]);
Child(fd[1]);
puts("Child process complete");
}
else
{
close(fd[1]);
Parent(fd[0]);
puts("Parent process complete");
}
}
void Child(pid_t Handle)
{
char buf[25];
FILE *ptr;
ptr = fopen("test.txt","r");
/*********** problem starts here *****************/
/* when I comment out the write statement and replace it with a */
/* printf("%c",c) the program outputs the text file. But the */
/* but the write statement does not work properly. */
write(Handle,buf,strlen(buf));
close(Handle);
}
void Parent(pid_t Handle)
{
char buf[25];
while(read(Handle,buf,25)>0)
{
printf("%s\n",buf);
}
}
Any help would be deeply appreciated. Thanks.