/*
* 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. :^(
typedefstruct 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;
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.