gpt4 book ai didi

c - 如何从文本文件中读取和保存数据?

转载 作者:行者123 更新时间:2023-11-30 20:32:48 24 4
gpt4 key购买 nike

我需要帮助了解如何将数据保存到变量中。如果我有 100 行数据怎么办?那我可能需要100个变量?请让我了解如何解决这个问题。非常感谢!

void show_addon(){
FILE *afp; //pointer for addon text file
char text_file[250];

afp = fopen("addon.txt", "r"); //opens txt file and READS only

puts("----------------------------------------------------");
puts(" --------------------ADD-ONS----------------------- ");
puts("----------------------------------------------------");

// printf("%s\n", text_file );
fscanf(afp, "%5[^:]:%[^:]:%f:%[^\n]\n", addon1.code, addon1.name, &addon1.price, addon1.description);
fscanf(afp, "%5[^:]:%[^:]:%f:%[^\n]\n", addon2.code, addon2.name, &addon2.price, addon2.description);
fscanf(afp, "%5[^:]:%[^:]:%f:%[^\n]\n", addon3.code, addon3.name, &addon3.price, addon3.description);
fscanf(afp, "%5[^:]:%[^:]:%f:%[^\n]\n", addon4.code, addon4.name, &addon4.price, addon4.description);
fscanf(afp, "%5[^:]:%[^:]:%f:%[^\n]\n", addon5.code, addon5.name, &addon5.price, addon5.description);

if ((afp = fopen("addon.txt", "r"))== NULL)
{
puts("File could not be found");
} else {
printf("Code : %s\n",addon1.code);
printf("Name : %s\n",addon1.name);
printf("Price : RM %.2f\n",addon1.price);
printf("Description : %s\n",addon1.description);
printf("---------------------------------------\n");
printf("Code : %s\n",addon2.code);
printf("Name : %s\n",addon2.name);
printf("Price : RM %.2f\n",addon2.price);
printf("Description : %s\n",addon2.description);
printf("---------------------------------------\n");
printf("Code : %s\n",addon3.code);
printf("Name : %s\n",addon3.name);
printf("Price : RM %.2f\n",addon3.price);
printf("Description : %s\n",addon3.description);
printf("---------------------------------------\n");
printf("Code : %s\n",addon4.code);
printf("Name : %s\n",addon4.name);
printf("Price : RM %.2f\n",addon4.price);
printf("Description : %s\n",addon4.description);
printf("---------------------------------------\n");
printf("Code : %s\n",addon5.code);
printf("Name : %s\n",addon5.name);
printf("Price : RM %.2f\n",addon5.price);
printf("Description : %s\n",addon5.description);
}
puts("------------------------------------------------------------------------------");

fclose(afp); //close txt file
return 0;
}

最佳答案

What if I had a data of 100 lines? Then I might need 100 variables?

不,您将使用数组来保存数据并使用 while 循环来读取数据。

类似于:

ADDON_TYPE addon[100];
int j;
if (!(afp = fopen("addon.txt", "r"))
{
printf("File error\n");
exit(1);
}

j = 0;
while (j < 100 &&
4 == fscanf(afp,
"%5[^:]:%[^:]:%f:%[^\n]\n",
addon[j].code,
addon[j].name,
&addon[j].price,
addon[j].description))
{
++j;
}

要打印值,您可以使用for循环。像这样的东西:

int i;
for(i = 0; i<j; ++i)
{
printf("Code : %s\n",addon[i].code);
printf("Name : %s\n",addon[i].name);
printf("Price : RM %.2f\n",addon[i].price);
printf("Description : %s\n",addon[i].description);
printf("---------------------------------------\n");
}

在许多情况下,您会使用动态数组(即使用 malloc 创建),以便在文件包含的条目数超过到目前为止已分配。

顺便说一句:在您的代码中,第二个 fopen 很奇怪 - 似乎是再次打开文件的错误

关于c - 如何从文本文件中读取和保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902501/

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