Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define GAMETITLE "The Game"
  6.  
  7. int main()
  8. {
  9.     system("clear");
  10.     printf("Welcome to %s\n",GAMETITLE);
  11.     menu();
  12. }
  13.  
  14. int menu(void)
  15. {
  16.     int option;
  17.    
  18.     printf("Please choose one of the following options\n");
  19.     printf("(1) New\n");
  20.     printf("(2) Load\n");
  21.     printf("(3) Exit\n");
  22.     printf("[]> ");
  23.     scanf("%d",&option);
  24.     if(option < 1 || option > 3)
  25.     {
  26.         error_msg("You didn't choose one of the options!\n");
  27.     }
  28.     else
  29.     {
  30.         switch(option)
  31.         {
  32.             case 1: new(); break;
  33.             case 2: load(); break;
  34.             case 3: printf("Thanks for playing!\n"); exit(1);
  35.         }
  36.     }
  37.    
  38. }
  39.  
  40. int new(void)
  41. {
  42.     char user[15], pass[15];
  43.        
  44.     FILE *fp;
  45.     if((fp = fopen("../data/profile.db","r"))==NULL)
  46.     {
  47.         printf("Desired username: ");
  48.         scanf("%s",user);
  49.         printf("Desired password: ");
  50.         scanf("%s",pass);
  51.         fp = fopen("../data/profile.db","a+");
  52.         fprintf(fp,"%s,",user);
  53.         fprintf(fp,"%s,",pass);
  54.         printf("Profile sucessfully created!\n");
  55.     }
  56.     else
  57.     {
  58.         printf("Profile already exists!\n");
  59.     }
  60.     fclose(fp);
  61. }
  62.  
  63. int load(void)
  64. {
  65.     FILE *fp;
  66.     char fd;
  67.     char result[256][32];
  68.    
  69.     if((fp = fopen("../data/profile.db","r"))==NULL)
  70.     {
  71.         error_msg("Cannot open file!\n");
  72.         exit(1);
  73.     }
  74.     else
  75.     {
  76.         while((fd = fgetc(fp))!=NULL)
  77.         {
  78.             printf("%s",fd);
  79.         }
  80.         fclose(fp);
  81.    
  82.         split(fd,result);
  83.        
  84.         printf("The profile.db was successfully retrieved\n");
  85.         printf("Username: %s",result[0]);
  86.         printf("Password: %s",result[1]);
  87.  
  88.     }
  89. }
  90.  
  91. int split(char *str, char splitstr[256][32])
  92. {
  93.     char *p;
  94.     p = strtok(str,",");
  95.     int i=0;
  96.    
  97.     while(p)
  98.     {
  99.         strcpy(splitstr[i++],p);
  100.         p = strtok(NULL,",");
  101.     }
  102.     return i;
  103. }
  104.  
  105. int error_msg(char *error)
  106. {
  107.     printf("Sorry! %s",error);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement