[英]Can't read a structure from text file - C
我正在尝试制作一个 function,它会从文本文件中读取结构并将其打印出来。 但是,我的 while 循环不知何故不起作用,我不知道为什么:
#include <stdio.h>
#include <stdlib.h>
struct data {
char foodName[FILENAME_MAX];
int rating;
double price;
};
FILE* openFile(char antraste[], char rezimas); <--- this function works ant it just opens a text file, so don't look into this
int main() {
FILE* dataText = openFile("Input data file: ", 'r');
struct data food;
while(fread(&food, sizeof(struct data), 1, dataText)) <--- it doesn't go inside the cycle
{
printf ("name = %s rating = %d price = %d\n", food.foodName, food.rating, food.price);
}
fclose(dataText);
printf("Done\n");
return 0;
}
我的 data.txt 文件如下所示:
Pasta 4.5 2.5
Soup 3.4 1.4
Pie 4.8 3.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.