简体   繁体   English

从C中的文本文件读取格式化的数据

[英]Read formatted data from text file in C

What would be the best way to read this data from a txt file? txt文件读取此数据的最佳方法是什么?

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 只需使用user3121023提到的fscanf

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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