Advertisement
Guest User

Edited Import_Export_Player_Database.c

a guest
Mar 22nd, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. // It is not the actual program...
  2. // Just a pieace of code to understand what i want to do...
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. // Structure that stores player name and score...
  9. typedef struct {
  10.     char name[32];
  11.     int score;
  12. } Player;
  13.  
  14. // Structure that stores number of players and database of all players...
  15. typedef struct {
  16.     Player* players;
  17.     int no_of_players;
  18. } Database;
  19.  
  20. // Function that stores database in binary mode...
  21. void save_data(Database* o) {
  22.     FILE* file = fopen("database.dat","wb");
  23.     fwrite(o->players, sizeof (Player), o->no_of_players, file);
  24.     fclose(file);
  25. }
  26.  
  27. // Function that reads database in binary mode...
  28. void get_data(Database* o) {
  29.     /* Note: should check if fopens is successful. It's segfault if not */
  30.     FILE* file = fopen("database.dat","rb");
  31.     Player player;
  32.     /* In case we dont know beforehand how many players are saved,
  33.      * read one record at a time */
  34.     while (fread(&player, sizeof (Player), 1, file) > 0) {
  35.         o->players = realloc(o->players, sizeof (Player) * (o->no_of_players + 1));
  36.         memcpy(&o->players[o->no_of_players], &player, sizeof (Player));
  37.         o->no_of_players++;
  38.     }
  39.     fclose(file);
  40. }
  41.  
  42. void Task_1_Take_And_Save_Data() {
  43.     Database data;
  44.     data.no_of_players = 3;
  45.     data.players = (Player*)malloc(data.no_of_players*sizeof(Player));
  46.  
  47.     strcpy(data.players[0].name,"Mukul");
  48.     strcpy(data.players[1].name,"Anil");
  49.     strcpy(data.players[2].name,"Govind");
  50.     data.players[0].score = 100;
  51.     data.players[1].score = 200;
  52.     data.players[2].score = 300;
  53.  
  54.     save_data(&data);
  55.     printf("Data Saved Successfully");
  56.     free(data.players);
  57. }
  58.  
  59. void Task_2_Read_And_Print_Data() {
  60.     Database data;
  61.     /* Initialize players database */
  62.     data.players = NULL;
  63.     data.no_of_players = 0;
  64.     get_data(&data);
  65.     for(int i=0; i<data.no_of_players; i++) {
  66.         printf("%s\n%d\n",data.players[i].name,data.players[i].score);
  67.     }
  68. }
  69.  
  70. int main() {
  71.     printf("1.Save Data\n2.Read Data\nEnter Choice ---> ");
  72.     int ch;
  73.     // Make Sure 1st Enter choice 1 to save data and then choice 2 on next run to read saved data...
  74.     scanf("%d",&ch);
  75.     switch(ch) {
  76.         case 1:
  77.             Task_1_Take_And_Save_Data();
  78.             break;
  79.         case 2:
  80.             Task_2_Read_And_Print_Data();
  81.             break;
  82.         default: printf("Oops! Invalid Choice...");
  83.     }
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement