I am reading an input file in the parent and based on the content in the file, I pass them to one of the two child processes.
char a[20],b[20],c[20];
pFile = fopen ("instruction.dat","r");
while(!feof(pFile))
{
fscanf(pFile,"%s %s %s",a,b,c);
switch(a[0])
{
case 'f': write(fds1[1],b,sizeof(b));
write(fds1[1],c,sizeof(c));
write(1,"\nAFRCF",6);
break;
case 'a': write(fds2[1],b,sizeof(b));
write(fds2[1],c,sizeof(c));
write(1,"\nAFRCA",6);
break;
default: write(1,err,sizeof(err));
//printf("\n Instruction.dat read error");
}
}
Now, the problem I am facing is that the sizeof(a) operator returns the size of the allocated memory to the array and thus the child process receives some garbage along with the actual contents from the input file. I can't predict the size of the array beforehand so I can't use dynamic allocation (malloc or realloc) to give the exact size of the array.
What is the way to pass the exact size of strings (after reading the input file) to the write() ?
write(among a million others) seems a more suitable title for the question, just saying :)