[英]How to read and save data from text file?
我需要幫助了解如何將數據保存到變量中。 如果我有 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;
}
如果我有 100 行的數據怎么辦? 那么我可能需要 100 個變量?
不,您將使用一個數組來保存數據和一個 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
創建),以便在文件包含的條目多於迄今為止分配的條目時調整其大小(即使用realloc
)。
順便說一句:在您的代碼中,第二個fopen
很奇怪 - 似乎是再次打開文件的錯誤
如果您可以使用 while 函數的FOR循環,那就更好了。它會一遍又一遍地迭代,所以您不想一次又一次地編寫代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.