简体   繁体   中英

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? Then I might need 100 variables? 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? Then I might need 100 variables?

No, you would use an array for holding data and a while loop for reading data.

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. 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.

BTW: In your code the second fopen is strange - seems like a bug to open the file again

如果您可以使用 while 函数的FOR循环,那就更好了。它会一遍又一遍地迭代,所以您不想一次又一次地编写代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM