【发布时间】:2021-05-24 12:50:11
【问题描述】:
我有一个逗号分隔的船列表及其规格,我需要将其读入结构。每行包含不同的船及其规格,因此我必须逐行阅读文件。
示例输入文件(我将使用的文件有 20 多行):
pontoon,Crest,Carribean RS 230 SLC,1,Suzuki,115,Blue,26,134595.00,135945.00,1,200,0,250,450,450,0
fishing,Key West,239 FS,1,Mercury,250,Orange,24,86430.00,87630.00,0,0,250,200,500,250,0
sport boat,Tahoe,T16,1,Yamaha,300,Yellow,22,26895.00,27745.00,0,250,0,0,350,250,0
我有一个链表 watercraft_t:
typedef struct watercraft {
char type[15]; // e.g. pontoon, sport boat, sailboat, fishing,
// canoe, kayak, jetski, etc.
char make[20];
char model[30];
int propulsion; // 0 = none; 1 = outBoard; 2 = inBoard;
char engine[15]; // Suzuki, Yamaha, etc.
int hp; // horse power
char color[25];
int length; // feet
double base_price;
double total_price;
accessories_t extras;
struct watercraft *next;
} watercraft_t;
我的主函数打开文件并将其存储在指针中:
FILE * fp = fopen(argv[1], "r"); // Opens file got from command line arg
然后将该文件传递给一个函数,该函数应该准确解析 1 行,然后返回该节点以放置在链表中。
// Create watercrafts from the info in file
watercraft_t *new_waterCraft( FILE *inFile )
{
watercraft_t *newNode;
newNode = (watercraft_t*)malloc(sizeof(watercraft_t));
fscanf(inFile, "%s %s %s %d %s %d %s %d %lf %lf", newNode->type, newNode->make, newNode->model, &(newNode->propulsion), newNode->engine, &(newNode->hp), newNode->color, &(newNode->length), &(newNode->base_price), &(newNode->total_price));
return newNode;
}
当调用函数仅打印每艘船的类型时,结果如下:
1. pontoon,Crest,CRS
2. SLC,1,Suzuki,11fishing,Key
3. SLC,1,Suzuki,11fishing,Key
4. SLC,1,Suzuki,11fishing,Key
5. SLC,1,Suzuki,11fishing,Key
6. SLC,1,Suzuki,11fishing,Key
7. SLC,1,Suzuki,11fishing,Key
8. SLC,1,Suzuki,11fishing,Key
9. SLC,1,Suzuki,11fishing,Key
10. SLC,1,Suzuki,11fishing,Key
11. SLC,1,Suzuki,11fishing,Key
12. SLC,1,Suzuki,11fishing,Key
13. SLC,1,Suzuki,11fishing,Key
14. SLC,1,Suzuki,11fishing,Key
15. SLC,1,Suzuki,11fishing,Key
16. SLC,1,Suzuki,11fishing,Key
17. SLC,1,Suzuki,11fishing,Key
我已将问题缩小到如何使用 fscanf 从文件中读取值。
我尝试的第一件事是在所有占位符之间使用 %*c,但在运行之后,我的输出看起来完全一样。接下来我意识到我将无法使用 fscanf,因为文本文件将包含需要读取的空格。
我的下一个想法是使用 fgets,但我认为我也不能使用它,因为我不确定每次必须读取多少个字符。我只需要它在行尾停止读取,同时用逗号分隔值。
我已经寻找了几个小时的答案,但到目前为止似乎没有任何效果。
【问题讨论】:
-
这能回答你的问题吗? Read .csv file in C
标签: c struct io scanf text-parsing