{
char filePath[MAX_PATH];
char databuf[1024];
::GetCurrentDirectory(MAX_PATH,filePath);
strcat(filePath,"\\");
strcat(filePath,"config.ini");
int count=CalcCount(); //统计目前ini文件中已有的节数--见下面
CString secName="Section";
CString temp;
temp.Format("%d",count-1);
secName+=temp;
::GetPrivateProfileString(secName,"user id","",databuf,1024,filePath);
m_UserName=databuf;
::GetPrivateProfileString(secName,"password","",databuf,1024,filePath);
m_Passwd=databuf;
return FALSE;
}
BOOL CTest131Dlg::WriteIni()//写ini
{
char filePath[MAX_PATH];
::GetCurrentDirectory(MAX_PATH,filePath);
strcat(filePath,"\\");
strcat(filePath,"config.ini");
int count=CalcCount();//同上
CString secName="Section";
CString temp;
temp.Format("%d",count);
secName+=temp;
::WritePrivateProfileString(secName,"user id",m_UserName,filePath);
::WritePrivateProfileString(secName,"password",m_Passwd,filePath);
return TRUE;
}
int CTest131Dlg::CalcCount()
{
char chSectionNames[1024]={0};
char *pSectionName;//保存找到的某个节名字符串的首地址
int i; //指向数组chSectionNames的某个位置,从0开始,顺序后裔
int j=0;//用来保存下一个节名字符串的首地址
int count=0; //统计节的个数
::GetPrivateProfileSectionNames(chSectionNames,1024,".\\config.ini");
for(i=0;i<1024;i++,j++)
{
if('\0'==chSectionNames[0])
break;
if('\0'==chSectionNames[i])
{
pSectionName=&chSectionNames[i-j];
j=-1;
//AfxMessageBox(pSectionName);
count++;
if('\0'==chSectionNames[i+1])
{
break;
}
}
}
return count;
}