| INI files are often used in configuration files. In VC, their functions are:
Write the. ini file: bool writeprivateprofilestrinG (maid, maid );
Read the. ini file: DWORDGetprivateprofilestring(Lptstr lpappname, lptstr lpkeyname, lptstr lpdefaut, lpstr lpreturnedstring, DWORD nsize, lptstr lpfilename );
Read integer value: uint getprivateprofileint (lpappname, lptstr lpkeyname, int ndefault, lptstr lpfilename );
The meanings of the parameters are as follows:
Lptstr lpappname ------- a field name in the INI File
Lptstr lpkeyname -------- a key name under lpappname, that is, the specific variable name in it
Lpctstr lpstring --------- is the key value, that is, the value of the variable. It must be of the type of lpctstr or cstring.
Lpctstr lpfilename -------- complete INI file path name
Lpctstr lpdefaut ---------- if there is no value for the first two parameters, this value is assigned to the variable
Lpstr lpreturnedstring -------- cstring object that receives the value in the INI file, that is, the receiving buffer
DWORD nsize ------ size of the receiving buffer
Example:
Cstring strname, strtemp;
Int Nage;
Strname = "Jacky ";
Nage = 13;
WriteprivateprofilestrinG ("student", "name", strname, "C: \ setting. ini ");
Result: The following figure is displayed in the INI file :)
[Student]
Name = Jacky
Read:
Cstring sname;
Getprivateprofilestring("Student", "name", "defaultname", sname. getbuffer (max_length), max_length, "C: \ setting. ini ");
Result: sname = "Jacky". Note that the function must be released after the getbuffer function is used up (sname is used. releasebuffer () function), otherwise other subfunctions that use sname will fail.
Reading integers is relatively simple, as shown below:
Int result = getprivateprofileint ("student", "Nage", 0, "C: \ setting. ini") the returned value is the read result!
InGetprivateprofilestringThe last parameter is the configuration file path parameter, which can only be an absolute path, not a relative path, but now I need to be able to include my EXE file with my configuration file. So I used the getcurrentdirectory function.
OriginalCodeAs follows:
Cstring server_ip;
Cstring des = "";
: Getcurrentdirectory (max_pathlength, Des. getbuffer (max_pathlength ));
Des. releasebuffer ();
Des + = "\ config. ini "; Getprivateprofilestring("Phonedemo", "server_ip", "", server_ip.getbuffersetlength (15), 15, des );
Server_ip.releasebuffer ();
Note: when using the cstring variable here, after using the getbuffer function, you must use the releasebuffer () function to perform other operations such as string +.
|