共用自己寫的測試ini 檔案讀寫源碼

來源:互聯網
上載者:User

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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.