Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define GAMETITLE "The Game"
- int main()
- {
- system("clear");
- printf("Welcome to %s\n",GAMETITLE);
- menu();
- }
- int menu(void)
- {
- int option;
- printf("Please choose one of the following options\n");
- printf("(1) New\n");
- printf("(2) Load\n");
- printf("(3) Exit\n");
- printf("[]> ");
- scanf("%d",&option);
- if(option < 1 || option > 3)
- {
- error_msg("You didn't choose one of the options!\n");
- }
- else
- {
- switch(option)
- {
- case 1: new(); break;
- case 2: load(); break;
- case 3: printf("Thanks for playing!\n"); exit(1);
- }
- }
- }
- int new(void)
- {
- char user[15], pass[15];
- FILE *fp;
- if((fp = fopen("../data/profile.db","r"))==NULL)
- {
- printf("Desired username: ");
- scanf("%s",user);
- printf("Desired password: ");
- scanf("%s",pass);
- fp = fopen("../data/profile.db","a+");
- fprintf(fp,"%s,",user);
- fprintf(fp,"%s,",pass);
- printf("Profile sucessfully created!\n");
- }
- else
- {
- printf("Profile already exists!\n");
- }
- fclose(fp);
- }
- int load(void)
- {
- FILE *fp;
- char fd;
- char result[256][32];
- if((fp = fopen("../data/profile.db","r"))==NULL)
- {
- error_msg("Cannot open file!\n");
- exit(1);
- }
- else
- {
- while((fd = fgetc(fp))!=NULL)
- {
- printf("%s",fd);
- }
- fclose(fp);
- split(fd,result);
- printf("The profile.db was successfully retrieved\n");
- printf("Username: %s",result[0]);
- printf("Password: %s",result[1]);
- }
- }
- int split(char *str, char splitstr[256][32])
- {
- char *p;
- p = strtok(str,",");
- int i=0;
- while(p)
- {
- strcpy(splitstr[i++],p);
- p = strtok(NULL,",");
- }
- return i;
- }
- int error_msg(char *error)
- {
- printf("Sorry! %s",error);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement