BOOL CTest131Dlg::ReadIni() //讀ini
{
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;
}