分享

VC中INI文件读写 - 平淡才真实 - JavaEye技术网站

 wfsy1983 2010-09-26

在VC程序中利用系统提供的GetPrivateProfileString及WritePrivateProfileString函数直接读写系统配置ini文件(指定目录下的Ini文件)

假设在当前目录下有一个文件名为Tets.ini的文件
用于保存用户名和密码
文件格式如下:
[Section1]
Item1=huzhifeng
Item2=1234565

1.写INI文件
void CINI_File_TestDlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here

CString strSection        = "Section1";
   CString strSectionKey     = "Item1";
char strBuff[256];
   CString strValue        = _T("");
CString strFilePath;

strFilePath=GetCurrentDirectory(256,strBuff);   //获取当前路径
strFilePath.Format("%s\\Test.ini",strBuff);

GetDlgItemText(IDC_EDIT_NAME,strValue);      //获取文本框内容:即姓名
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath);   //写入ini文件中相应字段

strSectionKey="Item2";
GetDlgItemText(IDC_EDIT_PASSWORD,strValue);    //获取文本框内容:即密码
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath);
}

2.读INI文件内容
void CINI_File_TestDlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
CString strSection        = "Section1";
   CString strSectionKey     = "Item1";
char strBuff[256];
CString strValue        = _T("");
CString strFilePath;

strFilePath=GetCurrentDirectory(256,strBuff);   //获取当前路径
strFilePath.Format("%s\\Test.ini",strBuff);

GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath); //读取ini文件中相应字段的内容
strValue=strBuff;
SetDlgItemText(IDC_EDIT_NAME,strValue);

strSectionKey="Item2";
GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath);
strValue=strBuff;
SetDlgItemText(IDC_EDIT_PASSWORD,strValue);

UpdateData(FALSE);
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多