I'm having trouble on school assignment, currently the code works on child getting and putting text to new text file. except for the parent, file created successfully but its empty.
my text files look like this
1 text to be copied to child1.
0 text to be copied to parent
and my codes are below
int main(){
int c1,c2,c3 ;
FILE *fp1,*fpm, *fp2;
char str1[100];
char str2[100];
fp1 = fopen("test.txt","r");
fpm = fopen("mainlog.txt","w");
fp2 = fopen("child1log.txt","w");
c1 = fork();
if(c1 == 0){
printf("child Process\n");
while ((fgets(str1,80,fp1))!=NULL){
if(str1[0]=='1'){
fputs(str1+1,fp2);
}
}
}
else{
printf("This is parent Process\n");
while ((fgets(str,80,fp1))!=NULL){
if(str[0]=='0'){
fputs(str+1,fpm);
}
}
}
return 0;
}
Help is appreciated.