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

p.level is an integer, hence %d instead of %s ...

int viewcharacter()
{
    printf("Name:\t%s\n", p.name);
    printf("Level:\t%[B]d[/B]\n",p.level);
}
commented: Thanks for spotting my late night programming errors :) +1

p.level is an integer, hence %d instead of %s ...

int viewcharacter()
{
    printf("Name:\t%s\n", p.name);
    printf("Level:\t%[B]d[/B]\n",p.level);
}

Thank you, but it hasnt solved the problem but i think i know what the problem is, its the inputing from a file thats causing the problem.

as soon as i comment out the 2 lines of code that input the data my program runs perfectly no crashes.

i've tried using fgets() instead for the input but thats still crashes it :(

i then tried using fgets again this time writing what it reads to a global variable this worked perfectly. so i'm thinking that the problem lies with writing data to the variables inside my struct.

-Sad Matt :(

line 168: two problems:
1) doesn't look like malloc() was everf called to allocate space for name. That will most definitely crash the program.
2) you are passing a pointer to a pointer in that function. Remove the & because name is already a pointer.

Thank you very much Ancient, it works now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.