【发布时间】:2018-05-03 04:42:02
【问题描述】:
大家好,我有一个关于从文本文件中读取信息的快速问题。 我有一个格式化的 .txt,文件中有客户信息。该文件包含多个客户,每个客户都有不同的订单类型、购买的产品、地址、城市、州、总成本等。当我使用 fscanf() 时,它没有返回任何值,并且在调试器中分配给我的所有值变量不正确。
这是文本文件中的数据:
0
Programming With C
Larry Page
1600 Amphitheatre Parkway
Mountain View
California
94043
81.07
1
Data Structures and Algorithms in Java
Elon Musk
3500 Deer Creek Road
Palo Alto
California
94304
32.50
2
Computer Science Distilled
James Gosling
1234 Main Dr.
San Francisco
California
94122
35.70
这是我使用的代码。
int orderType, zipCode;
double totalCost;
char product[MAXSIZE], customer[MAXSIZE], address[MAXSIZE], city[MAXSIZE], state[MAXSIZE];
// Scan in the order type, product name, customer, address, city, state, zip code, order cost
fscanf(customerDataPtr, "%d", "%s", "%s", "%s", "%s", "%s", "%f", "%f");
printf("%d", "%s", "%s", "%s", "%s", "%s", "%f", "%f",
orderType, product, customer, address, city, state, zipCode, totalCost);
任何建议都将被视为非常有帮助。
【问题讨论】:
-
检查来自
fscanf()的返回值,并获得一个会抱怨滥用fscanf()的编译器——比如GCC。您需要一个格式字符串参数,其中包含一个字符串中的所有转换,然后是存储数据的指针参数。与printf()类似。 -
输入示例会很方便
-
数据中是否有空白行,或者这些行是连续的?这不是一个大问题(但它是一个问题)。我在编辑时查看数据时不清楚。
-
我在示例数据中看不到一个逗号。
fscanf的格式与数据不匹配
标签: c text-files scanf c-libraries