gpt4 book ai didi

C编程: Struct array construction while reading a file

转载 作者:行者123 更新时间:2023-11-30 20:15:46 25 4
gpt4 key购买 nike

我有一个问题,还有一些麻烦。我必须读取包含一些值的文件文本。该文件如下所示:

104 Olympus FE200   244.90
226 JVC MG155 944.20
342 Pentax OPTIOA20 344
509 Canon SELPHYES1 299.20
974 Canon IXUS800IS 444.50
.
.
.

344 Canon EOS500D 500

不同的字符串之间用“\t”分隔。

我必须编写一个函数来读取所有这些内容并创建一个由以下定义的结构数组:

struct product{
int id;
char constructor[MAX_SIZE];
char product_name[MAX_SIZE];
double price;
};

我实际上可以读取该文件,但是结构和结构选项卡变得越来越困难。有什么想法吗?

最佳答案

使用scanf():

struct product p;
scanf("%d %s %s %lf", &p.id, p.constructor, p.product_name, &p.price);

如果存在空格问题,请按照 @icktoofay 的建议使用 %[^\t\n] 而不是 %s

关于C编程: Struct array construction while reading a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16887978/

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