I need to parse input line into structure that contains union. In each line the 6th field is based on the previous (5th) field. Here is the sample data(with header):
ID Name Gender Age Job Class/Teaching
1001 Johnson M 18 S 501
1002 Lily F 19 S 302
1009 Lyon M 36 T Math
1010 Marilyn F 32 T Art
1011 Longha M 20 S 601
Tried to use structure array to hold the record of each line. fgets is used to read in each line which is then parsed with sscanf to fill the members of the structure. The 6th field (Class/Teaching) contains string or integer based on the 5th field (Job), so that a union is used to hold the data in this field. Following is my code:
struct info {
int id;
char name[20];
char gender;
int age;
char job;
union { /*Use union inside the struct */
int classNumber;
char Teaching[10];
} category;
};
typedef struct info INFO; /*Define INFO in place of struct info*/
INFO person[15] = {0};
char line[256];
unsigned int i = 0; //increment counter
while (fgets(line, 256, stdin) != NULL) {
sscanf(line, "%d %s %c %d %c", &(person[i].id), person[i].name, &(person[i].gender), &(person[i].age), &(person[i].job));
if (person[i].job == 'T') { /*according to the Job field, receive different data types in the union*/
sscanf(line, "%d %s %c %d %c %s",
&(person[i].id), person[i].name, &(person[i].gender), &(person[i].age),
&(person[i].job), person[i].category.Teaching);
}
else if (person[i].job == 'S') {
sscanf(line, "%d %s %c %d %c %d",
&(person[i].id), person[i].name, &(person[i].gender), &(person[i].age),
&(person[i].job), &(person[i].category.classNumber));
}
i++;
}
Because the 6th field (Class/Teaching) is based on the 5th field (Job), so I used sscanf() twice to parse the line. The first sscanf parses the first 5 fields (until Job field) to decide which type of data should be used for the 6th field (Class/Teaching). The second sscanf parses all the 6 fields till the end of each line.
The code seems working (attached is the full code with sample data), but I was wondering:
- Is this the correct way to do the job because sscanf() is used twice and the first 5 fields are scanned twice?
- I was thinking the way to parse the line till the 5th field, check its type, then continue to parse the 6th field so that each line is scanned only once. How to achieve this?
For practice I push myself using pure C only. Thanks in advance.
struct18_forum.txt (1.8 KB)