#include #include #include #include #include #include #include //defines the O_WRONLY constant, et al using namespace std; int child(); int fd[2]; int child() { /*Chaque fils devra 1) ouvrir le pipe en écriture 2) ouvrir le fichier en lecture 3) lire le fichier et écrire dans le pipe 4) fermer le pipe 5) se terminer proprement (via exit) __________________*/ char path1i[100]; int fds, number_read_in; char buffer[256]; close(fd[0]); printf("whose text you want to ad?:\n"); dup2(fd[1],1); scanf("%s", path1i); fds= open(path1i, O_RDONLY); if(fds<0)//if we fail to open the file { printf("file not opened"); exit( 1); } //now we will read the contents of the file number_read_in = read(fds,buffer,255); //size of buffer is 256 close(fds); buffer[number_read_in]=NULL;//so the printf will work printf("%d \n", number_read_in); printf("%s\n", buffer); close(fd[1]); exit(0); } int main() { /* Processus Pere:Le père devra lire le pipe et écrire dans fichier texte */ int s, i; char str[20]; if (pipe(fd) != 0) /* fd devient descripteur de fichier */ { fprintf(stderr,"Problemes dans l'ouverture de Pipe \n"); exit(1); } /* Lancement d'un fils */ if ( fork() == 0 ) { /* Processus Fils 1 */ child(); } close(fd[1]); dup2(fd[0],0); while(gets(str)) //get from pipe { printf("I am the father process.\n"); printf("%s\n",str); //print to std out (the screen this time) } close(fd[0]); printf("fin du programme\n"); exit(EXIT_SUCCESS); }