Operating with Visual C ++ INI file collection

 In the program we write them, there is always some configuration information needs to be saved to complete the functions of the program, the easiest way is to write the information in the INI file, read into the program initialization specific applications as follows:.

  . A will information writing .INI file.

  WINAPI 1. the function prototype is used:

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);


   Where the significance of the parameters:

   LPCTSTR lpAppName is a field name INI file.

   LPCTSTR lpKeyName is a key name in the lpAppName, popular talk is the variable name.

   LPCTSTR lpString is key, which is the value of a variable, but must LPCTSTR type or type CString

   LPCTSTR lpFileName is complete INI file name.

  2. specific use: set a current student, to be his name and age written c: /stud/student.ini file.

CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c://stud//student.ini");


   In this case c: /stud/student.ini file contents are as follows:

   [StudentInfo]
   the Name = Joe Smith

   saved the age of 3. To the students down, just the value of the integer character can be changed to:

strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c://stud//student.ini");

  Second, the information is read into the program variables from the INI file.

  WINAPI 1. The function prototype is used:

DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);


  Where the significance of the parameters:

   the first two parameters in the same sense WritePrivateProfileString.

    LpDefault: If the INI file is not the first two parameters specified field name or key name, then this is assigned to the variable.

    LpReturnedString: receiving INI file CString object of value, that is the purpose of the buffer.

   nSize: the purpose of the buffer size.

   lpFileName: INI file name is complete.

  2. specific use: now want to step on the student's written information is read into the program .

CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name"," 默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c://stud//student.ini");


   After execution strStudName values: "John Doe", the first two parameters when the error, which is:. "Default Name"

  3 reads an integer value WINAPI use another function:

UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);


   Here the meaning of the parameters using the same method as follows:

nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c://stud//student.ini");

   Three write cycle a plurality of values, a set of existing programs, the file name saved To several recently used, the specific procedure is as follows:

  1. Write:

StrTemp CString, strTempA;
int I;
int = nCount. 6;
File: // There are six need to save a file name
for (I = 0; I
{strTemp.Format ( "% D", I);
strTempA = filename;
File : // file name can be obtained from the array, list boxes, etc..
:: WritePrivateProfileString ( "UseFileName", "fileName" + strTemp, strTempA,
"c: //usefile//usefile.ini");
}
strTemp.Format ( "% D", nCount);
:: the WritePrivateProfileString ( "the FileCount", "the Count", strTemp, "C: //usefile//usefile.ini");
file: // write the total number of files, to read out.


  2. read:

nCount=::GetPrivateProfileInt("FileCount","Count",0,"c://usefile//usefile.ini");
for(i=0;i
{strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c://usefile//usefile.ini");

file:// 使用strTempA中的内容.

}


  To add four points:

   the path 1.INI file must be complete, in front of the filename directory must exist at all levels, otherwise write is not successful, the function returns FALSE value.

    Path 2. File names must be in //, because in VC ++ in // represents only a /.

   3. INI file may also be placed in the program directory, as at this time lpFileName parameters:. ".//student.ini"

   4. paste source code from the web page, it is best to pasted into Notepad, go down to the VC paste, otherwise easily lead to compile errors, at the beginning I am also very puzzled, not how good the code for it? only later did find this method. there are some of the code used in the full-width characters such as : <, \ and so on, it can also cause compile errors.

Reproduced in: https: //my.oschina.net/dake/blog/196831

Guess you like

Origin blog.csdn.net/weixin_33877092/article/details/91586183