1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName );
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中.
CString strName,strTemp; int nAge; strName="张三"; nAge=12; ::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge); ::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");
1.所用的WINAPI函数原型为:
DWORD GetPrivateProfileString( LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName );
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName; int nStudAge; GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
3.读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt( LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName );
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini"); |
1.写入:
CString strTemp,strTempA; int i; int nCount=6; //{代码}file:共有6个文件名需要保存 for(i=0;i<nCount;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:将文件总数写入,以便读出.2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");for(i=0;i<NCOUNT;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中的内容.}
补充2点:
1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
2.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\\student.ini".