Posted By: zSteam
how to read and write into an ini file - 05/07/09 18:56
Concering this post I have extended the scripts!
First of all I'm not be able to read out the whole ini file via windows commands.
But MrGuest wrote a "work around".
Here are the scripts:
config.c
main script
config.ini
First of all I'm not be able to read out the whole ini file via windows commands.
But MrGuest wrote a "work around".
Here are the scripts:
config.c
Code:
//////////////////////////////////////////////////////////////////////////////////////////////////// // read the whole ini file //////////////////////////////////////////////////////////////////////////////////////////////////// STRING* str_file_data = ""; var _fHandle_user_config_i = NULL; void get_user_settings(STRING* local_filename) { _fHandle_user_config_i = file_open_read(local_filename); if(_fHandle_user_config_i){ //read config file int eof = 0; while(eof != -1){ eof = file_str_read(_fHandle_user_config_i, str_file_data); var_for_name(str_file_data); } } file_close(_fHandle_user_config_i); } //////////////////////////////////////////////////////////////////////////////////////////////////// // read single/predefinded variables //////////////////////////////////////////////////////////////////////////////////////////////////// //predefinded variables char result_chr[32]; int version_; void read_ini(STRING* local_filename) { STRING* path_to_ini = "#256"; //set path str_cpy(path_to_ini,work_dir); str_cat(path_to_ini,"\\"); str_cat(path_to_ini,local_filename); // section, key, default value, predefined string,size, file GetPrivateProfileString("header", "game_name", "can't read string", result_chr, 32, _chr(path_to_ini)); // var = section, key, default value, file version_ = GetPrivateProfileInt("header", "version", 0, _chr(path_to_ini)); } //////////////////////////////////////////////////////////////////////////////////////////////////// // write single/predefinded variables //////////////////////////////////////////////////////////////////////////////////////////////////// void write_ini(STRING* local_filename, STRING* section, STRING* variable) { STRING* path_to_ini = "#256"; STRING* variable_value = "#32"; str_cpy(path_to_ini,work_dir); str_cat(path_to_ini,"\\"); str_cat(path_to_ini,local_filename); str_cpy(variable_value,variable); var_for_name(variable_value); str_clip(variable_value,(str_len(variable)+3)); WritePrivateProfileString(section, variable, _chr(variable_value), _chr(path_to_ini)); }
main script
Code:
snd_vol_menu=50; //ini file, section, variable write_ini("config.ini", "soundsettings", "snd_vol_menu");
config.ini
Code:
[soundsettings] snd_vol_bomb=500 snd_vol_crate=250 snd_vol_song=0 snd_vol_menu=0