If you know for sure that the input file will be in a well-formed, very specific format, fscanf()it is always an option and will do most of the work for you. Below I use sscanf()instead just for illustration without the need to create a file. You can change the call to use fscanf()for your file.
#define MAXSIZE 32
const char *line = "Add id:324 name:\"john\" name2:\"doe\" num1:2009 num2:5 num3:20";
char op[MAXSIZE], name[MAXSIZE], name2[MAXSIZE];
int id, num1, num2, num3;
int count =
sscanf(line,
"%s "
"id:%d "
"name:\"%[^\"]\" "
"name2:\"%[^\"]\" "
"num1:%d "
"num2:%d "
"num3:%d ",
op, &id, name, name2, &num1, &num2, &num3);
if (count == 7)
printf("%s %d %s %s %d %d %d\n", op, id, name, name2, num1, num2, num3);
else
printf("error scanning line\n");
Outputs:
Add 324 john doe 2009 5 20
, , , - fgets() . , . , strtok() .