Teen Programmers Unite  
 

 

Return to forum top

Config loader

Posted by gian [send private reply] at September 14, 2001, 10:55:17 PM

Okay... whilst trying to create a config loader, I have run into numerous problems... have a look at the following example code:

typedef struct {
char *main_directory;
char *mod_conf_filename;
} XIE_CONFIGFILE;

XIE_CONFIGFILE xie_cfg;


int read_main_cfg( void )
{
FILE * fp = fopen("C:\\xie\\newest\\conf\\xie.conf", "rb");
if(fp) fread(&xie_cfg, sizeof(XIE_CONFIGFILE), 1, fp);
if(fp) fclose(fp);
return 1;
}

int write_main_cfg( void )
{
FILE * fp = fopen("C:\\xie\\newest\\conf\\xie.conf", "wb");
if(fp) fwrite(&xie_cfg, sizeof(XIE_CONFIGFILE), 1, fp);
if(fp) fclose(fp);
return 1;
}

main()
{
xie_cfg.mod_conf_filename = "blablabla";
write_main_cfg();
}

This produces a small string in the xie.conf file: ıB ııB ... fairly normal for a binary file... I guess....

Later, when using the read_main_cfg() function, the output is: 1: The var %s The location %d 2: (Null) ... which is bizzare, because the code used that produces that is simply:
read_main_cfg();
printf("1: %s 2: %s\n", xie_cfg.mod_conf_filename, xie_cfg.main_directory);

Any suggestions, or things would be appreciated, and not just a mocking series of cynical replies...

Posted by taubz [send private reply] at September 15, 2001, 10:47:16 AM

Your data structure is composed of two pointers instead of char buffers.... You're writing memory locations to file. :)

- taubz

Posted by gian [send private reply] at September 17, 2001, 03:02:35 PM

Thanks man... I knew it would be something silly like that.

Thanks!

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.