Hi,
I'm writing a program where the user can enters numbers and these numbers are stored in a file, then the numbers are read from the file and a certain calculation is done to each number by creating a thread for each
My problem is:
The first problem is whenever I enter a number with more the one digit the program won't read it and will terminate, here are the functions I'm using
/*Function for the user interface*/
void process_child(int input)
{
FILE *fp;
fp=fdopen(input,"w");
if(fp==NULL)
{
perror("fdopen");
exit(0);
}
/*get the input from the user*/
for(;;){
char user_input[20]; /*assuming a max of 20 numbers is entered*/
fgets(user_input,sizeof(input),stdin);
if (user_input[0]!='0' && user_input[1]!='\n')
break;
fputs(user_input,fp);
}
}
void process_parent(int output)
{
FILE *fp;
int i;
fp=fdopen(output,"r");
if(fp==NULL)
{
perror("fdopen");
exit(0);
}
/*read from the file*/
for(;;){
char buf[20];
char *p;
int number;
p=fgets(buf,sizeof(buf),fp);
/*check for end of file*/
if(p==NULL)
break;
/*convert string to integer*/
sscanf(buf, "%d", &i);
printf("%d\n",i);
process_thread(i);
}
}