简体   繁体   中英

C fscanf input format

I have an input file that contains lines of the following format:

 %s %d %d %d %lf %lf ... %lf\n

where the number of double values is unknown, but for my calculations I accept only first 15 of them.

The problem I can't figure out is, when I get to the line like this:

City0 28 2 2016 1 2 3 4 5 6 7 8 9 10
City1 28 2 2016 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
City2 1 3 2016 1 2 3 4 5

and I correctly assign respective values to a certain structure, I get the following:

City0 28 2 2016 Number of measures: 10
City1 28 2 2016 Number of measures: 15
16 17 18 19 Number of measures: 1
City2 1 3 2016 Number of measures: 5

How do I read(to nowhere)/ignore everything until I get to the end of the line, and then start reading the next line as usual? I need the following output:

City0 28 2 2016 Number of measures: 10
City1 28 2 2016 Number of measures: 15
City2 1 3 2016 Number of measures: 5

I tried this but ain't got any more ideas:

i=0; char character;
while (fscanf(fp, "%s %d %d %d", c.name, &c[i].date.day, 
    &c[i].date.month, &c[i].date.year)==4 && i<number_of_cities) {
    while (fscanf(fp, "%lf", &c[i].measures[j])==1 && j<15) {
        j++;
    }
    if (j==15) {
        while (fscanf(fp, "%s", character)!='\n') {}
    }
    c[i].mnum = j;
    j=0;
    i++;
}

You can use fscanf() to read in an entire line of input, use sscanf() to scan the first four values, and use sscanf() again in a loop to read the double values. The trick here is to use the %n directive to save the position of the next read location in the string.

Here is an example. Note that size_t is used for array indices, as this is an unsigned integer type that is guaranteed to hold any array index. Also note that there is some error-checking when opening the file, and when scanning the beginning of a line. If the initial elements of the line do not match expected values, the program exits with an error message. This error-checking could be tightened up a bit; for example, if the year is entered as a floating point value, such as 2016.0 , the input will be accepted, but the values stored in measures[] will begin with the 0 following the decimal point.

#include <stdio.h>
#include <stdlib.h>

struct Data {
    char name[1000];
    struct {
        int day;
        int month;
        int year;
    } date;
    size_t mnum;
    double measures[15];
};

int main(void)
{
    size_t i = 0, j = 0;
    char buffer[1000];
    char *read_ptr = buffer;
    int n_read = 0;

    size_t number_of_cities = 3;

    struct Data c[number_of_cities];

    FILE *fp;
    if ((fp = fopen("datafile.txt", "r")) == NULL) {
        fprintf(stderr, "Unable to open file\n");
        exit(EXIT_FAILURE);
    }

    while (fgets(buffer, 1000, fp) != NULL) {
        if (sscanf(buffer, "%s %d %d %d %n", c[i].name, &c[i].date.day, 
                   &c[i].date.month, &c[i].date.year, &n_read) != 4) {
            fprintf(stderr, "Incorrect input format\n");
            exit(EXIT_FAILURE);
        }
        read_ptr += n_read;
        while (sscanf(read_ptr, "%lf %n", &c[i].measures[j], &n_read) == 1 &&
               j < 15) {
            read_ptr += n_read;
            ++j;
        }
        c[i].mnum = j;
        ++i;        
        j = 0;
        read_ptr = buffer;
        if (i == number_of_cities) {
            break;
        }
    }

    for (i = 0; i < number_of_cities; i++) {
        printf("%s %d %d %d Number of measures: %zu\n",
               c[i].name,
               c[i].date.day, c[i].date.month, c[i].date.year,
               c[i].mnum);
        for (j = 0; j < c[i].mnum; j++) {
            printf("%5g", c[i].measures[j]);
        }
        putchar('\n');
    }

    return 0;
}

Program output using your example data as input:

City0 28 2 2016 Number of measures: 10
    1    2    3    4    5    6    7    8    9   10
City1 28 2 2016 Number of measures: 15
    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15
City2 1 3 2016 Number of measures: 5
    1    2    3    4    5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM