It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我有一个问题,还有一些麻烦。我必须阅读带有一些值的文件文本。该文件如下所示:

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);


如果空格是问题,请使用%[^\t\n]代替@cc,如@icktoofay所建议。

07-24 09:44
查看更多