We may often use the configuration file INI file to obtain or save the parameter information, in the VC its functions are mainly used in:
- Read characters
DWORD GetPrivateProfileString (
LPCTSTR lpappname, //INI file a field name [section name] can have a number of section names
LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside
LPCTSTR lpdefault, //If lpreturnedstring is empty, assign a variable to lpreturnedstring
LPTSTR lpreturnedstring, //pointer variable holding key value, receive buffer for receiving key values (data) in INI file
DWORD nSize, //lpreturnedstring buffer size
LPCTSTR path to lpFileName//INI file
);
- Read integer value (the return value is read to the whole)
UINT Getprivateprofileint (
LPCTSTR lpappname, //INI file a field name [section name] can have a number of section names
LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside
INT ndefault, //If the specified data return is not found, assign a variable value to the return value
LPCTSTR path to lpFileName//INI file
);
- Write characters
BOOL WritePrivateProfileString (
LPCTSTR lpappname, //INI file a field name [section name] can have a number of section names
LPCTSTR lpkeyname, //Lpappname A key name, that is, the specific variable name inside
LPCTSTR lpstring, //key value, i.e. data
LPCTSTR path to lpFileName//INI file
);
- Write integral type
- Read and write INI file relative path and absolute path can be, according to the actual situation to choose
".. \\IniFileName.ini "//This is a relative path
"D:\\inifilename.ini"//such as absolute path
- MAX_PATH: is the macro set by Microsoft's largest path-occupied byte
The INI file written is divided into: section, key name, key value
Give me a chestnut:
Xx.ini
[Serial port configuration]
Baud Rate =19200
With the theory, take a look at the demo of Practice:
LPTSTR Lppath = new Char[max_path];
strcpy (Lppath, "D:\\inifilename.ini");
WritePrivateProfileString ("Liming", "Sex", "Man", Lppath);
WritePrivateProfileString ("Liming", "Age", "a", "Lppath");
WritePrivateProfileString ("Fangfang", "Sex", "Woman", Lppath);
WritePrivateProfileString ("Fangfang", "Age", "$", Lppath);
delete [] lppath;
INI file is as follows:
[Liming]
Sex=man
Age=20
[Fangfang]
Sex=woman
Age=21
LPTSTR Lppath = new Char[max_path];
LPTSTR limingsex = new Char[6];
int limingage;
LPTSTR fangfangsex = new Char[6];
int fangfangage;
strcpy (Lppath, "... \\IniFileName.ini ");
GetPrivateProfileString ("Liming", "Sex", "" ", Limingsex, 6, Lppath);
Limingage = Getprivateprofileint ("Liming", "age", 0, Lppath);
GetPrivateProfileString ("Fangfang", "Sex", "" ", Fangfangsex, 6, Lppath);
Fangfangage = Getprivateprofileint ("Fangfang", "age", 0, Lppath);
delete [] lppath;
How to read and write INI configuration files (summary)