I am trying to integrate Clans into the game. I have it working for the most part but there seems to be an issue where if the game crashes/boots it is not reading the clan file properly. I believe its how the load_clan and save_clan functions are and how its not actually creating new lines in the file like it should. But I am not really sure how to correct it. I hope this is the reason anyway.
Code:
/* Loads the clans from the text file */
void load_clans(void)
{
FILE *fl;
int clannum = 0, line_num = 0, i = 0, tmp, curmem = 0;
char name[80];
char *ptr;
struct clan_type *cptr = NULL;
bool newc = FALSE;
char buf[MAX_STRING_LENGTH];
if ((fl = fopen(CLAN_FILE, "rt")) == NULL) {
fprintf(stderr, "SYSERR: Unable to open clan file!");
exit(0);
}
line_num += get_line(fl, buf);
if (sscanf(buf, "%d", clannum) != 1) {
fprintf(stderr, "Format error in clan, line %d (number of clans)\n", line_num);
exit(0);
}
/* Setup the global total number of clans */
cnum = clannum;
/* process each clan in order */
for (clannum = 0; clannum < cnum; clannum++) {
/* Get the info for the individual clans */
line_num += get_line(fl, buf);
if (sscanf(buf, "#%d", &tmp) != 1) {
fprintf(stderr, "Format error in clan (No Unique GID), line %d\n", line_num);
exit(0);
}
/* create some clan shaped memory space */
if ((cptr = enqueue_clan()) != NULL) {
cptr->number = tmp;
/* setup the global number of next new clan number */
if (!newc) {
if (cptr->number != clannum) {
newclan = clannum;
newc = TRUE;
}
}
if (newc) {
if (newclan == cptr->number) {
newclan = cnum;
newc = FALSE;
}
} else
newclan = cptr->number + 1;
/* allocate space for applicants */
for (i=0; i< 20; i++)
cptr->applicants[i] = NULL;
for (i=0; i< 100; i++)
cptr->members[i] = NULL;
/* Now get the name of the clan */
line_num += get_line(fl, buf);
if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
*ptr = '\0';
cptr->name = strdup(buf);
/* Now get the look member string */
line_num += get_line(fl, buf);
if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
*ptr = '\0';
cptr->member_look_str = strdup(buf);
/* Now get entrance room and direction and guard */
line_num += get_line(fl, buf);
if (sscanf(buf, "%d", &tmp) != 1) {
fprintf(stderr, "Format error in clan, line %d (entrance room)\n", line_num);
exit(0);
}
cptr->clan_entr_room = tmp;
/* Skip this line it's just a header */
line_num += get_line(fl, buf); // password header
line_num += get_line(fl, buf);
if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
*ptr = '\0';
cptr->clan_password = strdup(buf);
line_num += get_line(fl, buf); // gold header
line_num += get_line(fl, buf);
if (sscanf(buf, "%d", &(cptr->gold)) != 1)
{
fprintf(stderr, "Format error in clan (No Gold), line %d\n", line_num);
exit(0);
}
line_num += get_line(fl, buf); //leader header
/* Loop to get Members' Names */
for (;;)
{
line_num += get_line(fl, buf);
if (*buf == ';')
break;
sscanf(buf, "%s %d", name, &tmp);
if (tmp == CLAN_LEADER)
cptr->leadersname = strdup(name);
}
//line_num += get_line(fl, buf); // member Header
/* Loop to get Members' Names */
for (;;)
{
line_num += get_line(fl, buf);
if (*buf == ';')
break;
else if ((ptr = strchr(buf, '~')) != NULL)
*ptr = '\0';
cptr->members[curmem++] = strdup(buf);
}
curmem = 0;
// header caught by break statement
/* Okay we have the leader's name ... now for the applicants */
for (i = 0; i < 20; i++) {
line_num += get_line(fl, buf);
/* We're done when we hit the $ character */
if (*buf == '$')
break;
else if ((ptr = strchr(buf, '~')) != NULL) /* take off the '~' if it's there */
*ptr = '\0';
cptr->applicants[i] = strdup(buf);
}
} else break;
/* process the next clan */
}
/* done processing clans -- close the file */
fclose(fl);
}
void save_clans(void)
{
FILE *cfile;
int clannum = 0, i;
struct clan_type *cptr = clan_info;
if (cptr == NULL) {
fprintf(stderr, "SYSERR: No clans to save!!\n");
return;
}
if ((cfile = fopen(CLAN_FILE, "wt")) == NULL) {
fprintf(stderr, "SYSERR: Cannot save clans!\n");
exit(0);
}
order_clans();
/* The total number of clans */
fprintf(cfile, "%d\r\n", cnum);
/* Save each clan */
while (clannum < cnum && cptr != NULL) {
fprintf(cfile, "#%d\r\n"
"%s~\r\n"
"%s~\r\n"
"%d\r\n"
"; Password\r\n"
"%s~\r\n"
"; Gold\r\n"
"%d\r\n"
"; Leader\r\n"
"%s %d\r\n",
cptr->number, cptr->name,
cptr->member_look_str,
cptr->clan_entr_room,
cptr->clan_password,
cptr->gold,
cptr->leadersname, CLAN_LEADER);
fprintf(cfile, "; Members (Max of 100)\r\n");
if (cptr->members[0] != NULL)
{
for (i = 0; i < 100; i++) {
if (cptr->members[i] == NULL)
break;
else
fprintf(cfile, "%s~\r\n", cptr->members[i]);
}
}
fprintf(cfile, "; Applicants (Max of 20)\r\n");
if (cptr->applicants[0] != NULL) {
for (i = 0; i < 20; i++) {
if (cptr->applicants[i] == NULL)
break;
else
fprintf(cfile, "%s~\r\n", cptr->applicants[i]);
}
}
fprintf(cfile, "$\r\n");
/* process the next clan */
cptr = cptr->next;
clannum++;
}
/* done processing clans */
fclose(cfile);
}