[英]How to read and save data from text file?
I need help understanding how to save the data into variables.我需要帮助了解如何将数据保存到变量中。 What if I had a data of 100 lines?
如果我有 100 行的数据怎么办? Then I might need 100 variables?
那么我可能需要 100 个变量? Please give me some understanding on how I approach this problem.
请让我了解我如何解决这个问题。 Thank you so much!
非常感谢!
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?
如果我有 100 行的数据怎么办? Then I might need 100 variables?
那么我可能需要 100 个变量?
No, you would use an array for holding data and a while loop for reading data.不,您将使用一个数组来保存数据和一个 while 循环来读取数据。
Something like:就像是:
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;
}
To print the values you could use a for
loop.要打印值,您可以使用
for
循环。 Something like:就像是:
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");
}
In many cases you would use a dynamic array (ie created using malloc
) so that it could be resized (ie using realloc
) if the file contains more entries than allocated so far.在许多情况下,您会使用动态数组(即使用
malloc
创建),以便在文件包含的条目多于迄今为止分配的条目时调整其大小(即使用realloc
)。
BTW: In your code the second fopen
is strange - seems like a bug to open the file again顺便说一句:在您的代码中,第二个
fopen
很奇怪 - 似乎是再次打开文件的错误
如果您可以使用 while 函数的FOR循环,那就更好了。它会一遍又一遍地迭代,所以您不想一次又一次地编写代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.