MinorMapillai
MinorMapillai

Reputation: 199

Reading data separated by ';' from file

I am trying to read in data from a file that is formatted with ;. The data will always be of like this:

char[];int;int%;int

The char[] can have any number of spaces and the % should be disregarded when reading the data.

I am using fscanf() (I am allowed to use only that) for reading the data from the file.
Right now my code for that part of it is:

fscanf(file, "%[^;]%d%d%d", f_name, &f_id, &f_score, &f_section_num) != EOF)

Is there a regex for what I need? Or, how do I correct my fscanf?

Upvotes: 0

Views: 106

Answers (3)

Anand
Anand

Reputation: 374

The following code will allow you to read data separated by ; from your file:

char msg[100];
int  a;
char b[100];
int  c;

fscanf(fptr, "%[^;];%d;%[^;];%d", msg, &a, b, &c);
printf("%s\n %d\n %d\n %d\n", msg, a, atoi(b), c);

Upvotes: 0

David Collins
David Collins

Reputation: 3032

You could, alternatively, use strtok(). If, for example, you use a struct for each entry as follows,

typedef struct {
    char name[64];
    int id, score, section_num;
} entry_t;

the following would read each line of the file as follows.

char line[128] = {'\0'};
char *field = NULL;
entry_t entry;

while (fgets(line, sizeof(line), fp)) {
    field = strtok(line, ";");
    if (!field || strlen(field) > sizeof(entry.name)) continue;
    strcpy(entry.name, field);
    field = strtok(NULL, ";");
    if (!field) continue;
    entry.id = atoi(field);
    field = strtok(NULL, ";%");
    if (!field) continue;
    entry.score = atoi(field);
    field = strtok(NULL, ";");
    if (!field) continue;
    entry.section_num = atoi(field);
    // Do whatever you need with the entry - e.g. print its contents
}

I have removed some necessary boilerplate code for brevity. See http://codepad.org/lg6BJ0hk for a full example.

You can use strtol() instead of atoi() if you need to check the results of the integer conversions.

Upvotes: 0

Mathieu
Mathieu

Reputation: 9639

You can read the file using fscanf with this format string:

"%[^;];%d;%d%%;%d"
  • %[^;]: read up to first ;
  • ;: ignore the ;
  • %d: read one integer
  • ;: ignore the ;
  • %d: read one second integer
  • %%: ignore the %
  • ;: ignore the ;
  • %d: read one third integer

Do not forget to test the number of successful conversions made by fscanf by testing fscanf(...) == 4

So code will looks like:

FILE *f = fopen(...);
char name[64];
int i, integers[3];

while (fscanf(f, "%[^;];%d;%d%%;%d", name, &integers[0], &integers[1], &integers[2]) == 4)
{
    printf("name is %s\n", name);
    for (i = 0; i < 3; ++i)
    {
        printf("i[%d] = %d\n", i, integers[i]);
    }        
}
fclose(f);

Upvotes: 2

Related Questions