BOOL WritePrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName ); |
CString strName,strTemp; int nAge; strName="张三"; nAge=12; ::WritePrivateProfileString("StudentInfo","Name",strName,"c://stud//student.ini"); |
strTemp.Format("%d",nAge); ::WritePrivateProfileString("StudentInfo","Age",strTemp,"c://stud//student.ini"); |
二.将信息从INI文件中读入程序中的变量.
1.所用的WINAPI函数原型为:
DWORD GetPrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName ); |
CString strStudName; int nStudAge; GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c://stud//student.ini"); |
UINT GetPrivateProfileInt( LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName ); |
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c://stud//student.ini"); |
CString strTemp,strTempA; int i; int nCount=6; file://共有6个文件名需要保存 for(i=0;i {strTemp.Format("%d",i); strTempA=文件名; file://文件名可以从数组,列表框等处取得. ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA, "c://usefile//usefile.ini"); } strTemp.Format("%d",nCount); ::WritePrivateProfileString("FileCount","Count",strTemp,"c://usefile//usefile.ini"); file://将文件总数写入,以便读出. |
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中的内容. } |