简体   繁体   中英

Read formatted data from text file in C

What would be the best way to read this data from a txt file?

12   name1 2     1    65000
13   name2 5   3    30000
17   name3       2       3      30000
20   name4        3    2      58000

Notice that the whitespace may change with every line.

I was thinking in doing something like this:

while (fscanf(file, "%s\\S{1,}", string) != EOF)
{
    if (!isdigit(*string))
        printf("Name: %s\n", string);

    else if(*string != '0')
        printf("Number: %s \n", string);               
}

But, it seems over complicated and inefficient to dynamically store it.

Any other ideas?

Thanks

There is no need to use Regex.

Simply use fscanf as mentioned by user3121023

while ( fscanf ( file, "%d %s %d %d %d", &digit[i], &str[i], &number[i], &value[i]) == 4) { i++;}

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