fread seg fault

Hello!
I'm getting a "Segmentation fault (core dumped)" error message with a program I'm working on. Please help me figure out what is wrong.

The fault occurs with the fread() call in the following function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * PERSON load_player(void)
 * 	load a saved character
 * 	returns loaded character
 */
PERSON load_player(void)
{
	FILE *fp;
	PERSON iam;
	string name, path = "data/char/";

	system("dir data/char/");
	cout << "Load which character? ";
	cin >> name;
	path += name;

	/*open file*/
	if( (fp = fopen(path.c_str(), "rb")) == NULL )
	{
		cout << "Error opening file! " << path << endl;
		exit(1);
	}/*end fileopen error if*/

	if(fread(&iam, sizeof(PERSON),1,fp) != 1)
	{
		cout << "Error reading file! " << path << endl;
		fclose(fp);
		exit(1);
	}/*end fread err if*/

	fclose(fp);
	return iam;
}/*end load_player func*/


The file that i'm trying to load was created with this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * void savechr(string title, PERSON* man)
 *  save a character pointed to by man under the filename of title in the data/char/ directory
 */
void savechr(string title, PERSON* man)
{
     FILE *fp;
     string path = "data/char/" + title;

     /*open file*/
     if( (fp = fopen(path.c_str(), "wb")) == NULL )
     {
         cout << "Error opening file! " << path << endl;
         exit(1);
     }

     /*write info*/
     if( fwrite(man, sizeof(PERSON), 1, fp) != 1)
     {
         cout << "Error writing to file!\n";
         fclose(fp);
         exit(1);
     }

     fclose(fp);/*close file*/
}/*end savechr func*/


...wait a minute. As I type this I'm thinking that the problem is that my fwrite() call is saving the address of man. But, I tried changing it to fwrite(&man...)and still got the seg fault. :^(
How is PERSON defined?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef struct being{
	std::string name; 
	char start_class; 
	bool is_male;
	short mood;
	short men;            /*mental ability*/
	short phy;            /*physical ability*/
	short age;            /*years old*/
	short b_day[2];
	float hea;            /*helth*/
	float sk_pts;         /*skill points*/
	int   hp;             /*Hero Points*/
	float luc;            /*luck*/
	float mgt;            /*might*/
	short phy_skills[MAX_PHY_SKILLS]; /*10 but is 13*/
	short men_skills[MAX_MEN_SKILLS]; /*13*/
	short bth_skills[MAX_BTH_SKILLS]; /*12 but is 13*/
	short spls_known[MAX_SPLS]; /*12 but is 13*/
	short wep_profs[MAX_WEP_TYPE];   /*4:blade,axe,pole,bludgen*/
	short clothes[MAX_CLOTHING_TYPE];     /*3:quality#/shoes(yn)/cloak(yn)*/
	//struct being *spouse;
}PERSON;
Last edited on
Aha! I found the problem. It's with PERSON's string name. I changed it to a c-style character array with a defined length and used strcpy() to set it. Thanks for your help Peter87, I wouldn't have found it without examining PERSON closer.
Topic archived. No new replies allowed.