VC6.0 INI文件的读写
2013-01-02 22:55
155 查看
写在正文前: 由于在程序中需要加入读取INI文件的功能,找了一个介绍如何读取VC中读取INI的的代码。本来可以很顺利的写完 ,但是没想到花了比预期更多的时间。原本是本人对配置文件不怎么了解,以至于在 Item1=huzhifeng 这样的MAP后面加了个分号 ,即变为了 Item1=huzhifeng; 由此记下此文。 下面代码为转载:
在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);
}
在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);
}
相关文章推荐
- 读写INI文件的四个函数
- Qt读写ini文件的问题
- [c#]:如何在C#中读写INI文件(四)
- C#读写INI文件
- 读写Ini文件
- VC读写INI文件(转)
- 教你在C#中如何读写INI文件
- C中用函数读写ini文件的方法
- C#读写Ini文件
- C#如何读写ini文件
- Delphi中初始化.Ini文件的读写
- 使用Config::IniFiles读写ini文件
- C#调用win32 API读写INI文件
- 读写ini文件
- c# 读写 创建ini文件(简单)
- C#中如何读写INI文件
- Qt|Qt读写ini文件
- python读写ini文件示例(python读写文件)
- 用VB.net实现对.ini文件的读写操作的类
- Java读写.ini文件