gpt4 book ai didi

C - 逐行读取文件的最佳方法

转载 作者:行者123 更新时间:2023-11-30 21:06:02 25 4
gpt4 key购买 nike

Stack Overflow 上的拳头帖子。如果您必须读取由一行名称和 15 行数字组成的文件,那么该文件如下所示:

NAME1NUM1NUM2.........NUM15

also you don't know how many lines has the file (but they obviously will be a multiple of 16), what is the simplest way to read every line and put it in a structure like:

struct something {
char name[128];
int nums[15]:
}

我用了 while(fgets) 来读取每一行,直到 EOF 和 sscanf 来分析每一行并将其放入结构中,但我遇到了一些困难,例如双倍或因果数字。编辑:我无法发布代码,因为我是在大学 PC 上编写的,所以我现在无法访问这里

最佳答案

尝试fscanf (pFile, "%s %d %d %d (15 个整数的 15 次 %d,其他数据类型需要不同的 %...)", name, num1, num2, ....);

在某些时候 fscanf 会失败,然后您应该能够检索 EOF(使用不同的 fscanf、fgets 或其他)

关于C - 逐行读取文件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com