All pastes #583173 Raw Edit

Saving data on one variable ?

public text v1 · immutable
·published 2007-06-22 07:52 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){

int cp_process, fd1[2], fd2[2], n; 
char message1[200],message2[200],buffer[200];
int choice; 

if (pipe(fd1) == -1){  

perror("pipe");

exit(1);

}



if (pipe(fd2) == -1){ 

perror("pipe");

exit(1);

}

cp_process = fork(); 
if(cp_process == -1){
perror("Child was not created succesfully");
exit(2);
}

if(cp_process == 0){ 
close(fd1[0]); //close the reading side of pipe1
close(fd2[1]); //close the writing side of pipe2



printf("You have got the following choices:\n");
printf("1) Insert new student\n");
printf("2) Edit a student\n");
printf("3) Show a student details\n");
printf("4) Delete a student\n");
printf("Please type the number of your choice (1-4):\n");

scanf("%d", &choice);





if(choice == 1){
printf("\nEnter Student AM:\n");
scanf("%s",&buffer,10);
strcpy (message1,buffer);
printf("\nEnter Student Name:\n");
scanf("%s",&buffer,70);
strcat (message1,",");
strcat (message1,buffer);
printf("\nEnter Student Surname:\n");
scanf("%s",&buffer,140);
strcat (message1,",");
strcat (message1,buffer);
printf("\nEnter Student Semester:\n");
scanf("%s",&buffer,19);
strcat (message1,",");
strcat (message1,buffer);
write(fd1[1],message1,strlen(message1)+1);

read(fd2[0],message2,sizeof(message2));
wait(2);

printf("Parent sent %s",message2);

exit(2);
}
else if(choice == 2){
printf("Choice2");
}
else if(choice ==3){
printf("Choice3");
}
else if(choice == 4){
printf("Choice4");
}
else{
printf("Option not available. Terminating Program.\n");
exit(0);
}



}
else{



close(fd1[1]);

close(fd2[0]);

read(fd1[0],message1,sizeof(message1));

printf("I am the parent and I have received %s\n",message1);

write(fd2[1],message2,strlen(message2)+1);

exit(3);

}



}