Home > Mobile >  Parse string with multiple fields of a structure in C
Parse string with multiple fields of a structure in C

Time:08-04

Suppose I have a text file with a content similar to the following:

JohnWatsonAustralia25

And I have a structure like this:

struct Employee {
    char name[16];
    char last_name[16];
    char country[16];
    char age[16];
};

Is there any way to read all four strings in one instruction with fscanf? So that they are stored like this:

name = "John"
last_name = "Watson"
country = "Australia"
age = 25

For example, something like the following code but that separates the strings correctly (obviously that doesn't work as I want it to):

fscanf(file, "%s%s%s%s", name, last_name, country, age);

EDIT 1: The start of each text field is always a capital letter, there are no "McMahon" type fields with two capital letters. The first digit denotes the start of the age field and there are no spaces or any kind of delimiter character, all fields are joined as in the first example I put.

EDIT 2: All fields are at least two characters long and are not expected to exceed the maximum space of fifteen characters.

CodePudding user response:

It depends on what the rule, if any, is for how the string is to be separated into fields. If the first three fields are to be recognized as an initial capital Latin letter followed by between one and fourteen lowercase Latin letters, and the last field is to be recognized as a sequence of up to 15 decimal digits, then you could use variations on this:

struct employee e;

int num_fields = fscanf(file,
       " %1[A-Z][a-z]%1[A-Z][a-z]%1[A-Z][a-z][0-9]",
       e.name, e.name   1, e.last_name, e.last_name   1, e.country, e.country   1,
       e.age);

if (num_fields != 6) {
    // handle input error
}

Note that that will skip leading whitespace (explicitly, as %s would do implicitly), but it will not allow whitespace between the fields. It also assumes that you want each field of the structure to receive a null-terminated string, so that one character must be reserved for a terminator in all cases.

But really, don't do that. Surely you see how hard it is to read or understand. It would be better to be a bit more verbose by reading the whole input into a string and then parsing it from there.

  • Related