Well another problem, sorry for constantly asking.
My program is coming along well i think, it creates the file writes the data to it but it crashes when i try to view the information.
i'm not sure if its an error with my loaddata() function or my viewcharacter() function
#include <stdio.h>
#include <stdlib.h>
/* Maximum Name Size */
#define NAMESIZE 21
#define BUFFER 200
/* player structure */
typedef struct
{
char *name;
int level;
}player;
/* Global Variables */
FILE *data;
FILE *playerdata;
player p;
/* Prototypes */
/*prints out the menu and then returns the users menu choice*/
int mainmenu();
int gamemenu();
/*creates a new save datafile*/
int newgame();
/*loads an existing save datafile*/
int loadgame();
/*loads data from the text file into the character struct*/
int loaddata();
/*prints out the characters details*/
int viewcharacter();
/*closes all files ready for program exit*/
int exitgame();
int main(void)
{
/*variables*/
/*holds the users menu choice*/
int menu;
/*holds whether a character is loaded 1 = loaded*/
int charloaded;
/*display first menu*/
do
{
menu = mainmenu();
switch (menu)
{
case 1:
charloaded = newgame();
break;
case 2:
charloaded = loadgame();
break;
case 3:
return 0;
break;
}
}while (charloaded == 0); /*while no character is loaded loop*/
/*load data*/
loaddata();
/*display second menu*/
do
{
menu = gamemenu();
switch (menu)
{
case 1:
viewcharacter();
break;
case 2:
break;
case 3:
exitgame();
return 0;
break;
}
}while(menu != 3);
getchar();
return 0;
}
int mainmenu()
{
int menu;
printf("1 - Start New Game\n");
printf("2 - Load New Game\n");
printf("3 - Exit\n\n");
printf("Please Make Your Choice: ");
menu = inputinteger();
printf("\n\n");
return menu;
}
int gamemenu()
{
int menu;
printf("1 - View Character\n");
printf("2 - Continue Adventure\n");
printf("3 - Exit\n\n");
printf("Please Make Your Choice: ");
menu = inputinteger();
return menu;
}
int newgame()
{
char input[BUFFER];
char name[NAMESIZE];
int length;
printf("Please Enter Your Name\n");
printf("Maximum Length 20 characters: ");
fgets(input,BUFFER,stdin);
length = strlen(input); /*find the length of the name*/
if ( length > NAMESIZE )
{
printf("Error - Name to Long\n");
return 0;
}
else
{
strcpy(name,input);
}
length = strlen(name);
if (name[length-1] == '\n') /*check if last character is new line char*/
{
name[length-1] = 0; /*remove the newline char*/
}
playerdata = fopen(name, "w+"); /*create the new file*/
if (!playerdata)
{
printf("Error - Unable to Create File\n");
return 0;
}
else
{
fputs(name,playerdata); /*write the players name to the file*/
fputs ("\n",playerdata); /*new line*/
fputs("1",playerdata); /*write players level to file*/
}
return 1;
}
int loadgame()
{
char name[BUFFER];
int length;
printf("Please enter the name of the character you wish to load\n");
fgets(name,BUFFER,stdin);
length = strlen(name);
if (name[length-1] == '\n') /*check if last character is new line char*/
{
name[length-1] = 0; /*remove the newline char*/
}
playerdata = fopen(name, "r");
if (playerdata == NULL)
{
printf("Unable to Load File\n");
return 0;
}
return 1;
}
int loaddata()
{
fscanf(playerdata, "%s", &p.name);
fscanf(playerdata, "%d", &p.level);
return 0;
}
int viewcharacter()
{
printf("Name:\t%s\n", p.name);
printf("Level:\t%s\n",p.level);
}
int exitgame()
{
fclose(playerdata);
fclose(data);
}
Thanks again for anyone who can help me :)
-Matt