Sample input string
james 35 bond 30
berry 40 bond 37
The program must separte each input line into 4 parts. first name, age, last name, and age+7, the following code is a sample of with only 2 component, I don't know how to do it with 4 components. Could someone edit the code for me please.
Code:
void main(int argc, char *argv[])
{
struct person list[];
int i,n;
char *ch, *ch1;
char buf[256];
FILE *file1;
i=0;
fgets(buf,250,file1);
while(!feof(file1))
{
ch=strchr(buf,'\n');
if (ch!=NULL) *ch='\0';
else break;
ch = strchr(buf, ' ');
if(ch!=NULL)
{
*ch='\0';
ch1=++ch; //age
}
else break;
strcpy(list[i].lastname,buf);
list[i].age=atoi(ch1);
i++;
fgets(buf,256,file1);
}
n=i;
for(i=0;i<n;i++)
printf("%s is %d years, first name is %s\n", list[i].lastname, list[i].age);
}