MFC文件读写之ini文件
来源:互联网 发布:苹果电影下载软件 编辑:程序博客网 时间:2024/05/04 17:07
//向D盘写入程序数据库连接ini文件信息,默认设置如下
CFileFind finder; //查找是否存在ini文件,若不存在,则生成一个新的默认设置的ini文件,这样就保证了我们更改后的设置每次都可用
BOOL ifFind = finder.FindFile(_T("d:\\RoadDataManagerApp.ini"));
if( !ifFind )
{
::WritePrivateProfileStringW(_T("Database connection Info"),_T("IP"),_T("10.210.0.9"),_T("d:\\RoadDataManagerApp.ini"));
::WritePrivateProfileStringW(_T("Database connection Info"),_T("Database"),_T("RoadNetData"),_T("d:\\RoadDataManagerApp.ini"));
::WritePrivateProfileStringW(_T("Database connection Info"),_T("UID"),_T("sa"),_T("d:\\RoadDataManagerApp.ini"));
::WritePrivateProfileStringW(_T("Database connection Info"),_T("PWD"),_T("4814278"),_T("d:\\RoadDataManagerApp.ini"));
}
::GetPrivateProfileStringW(_T("Database connection Info"),_T("IP"),_T("没找到IP信息"),m_strCurrentIP.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));
::GetPrivateProfileStringW(_T("Database connection Info"),_T("Database"),_T("没找到数据库名信息"),m_strCurrentDBName.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));
::GetPrivateProfileStringW(_T("Database connection Info"),_T("UID"),_T("没找到用户名信息"),m_strCurrentUID.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));
::GetPrivateProfileStringW(_T("Database connection Info"),_T("PWD"),_T("没找到用户密码信息"),m_strCurrentPWD.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));
//在这里必须ReleaseBuffer(),否则无法再后面进行字符串的连接
m_strCurrentIP.ReleaseBuffer();
m_strCurrentDBName.ReleaseBuffer();
m_strCurrentUID.ReleaseBuffer();
m_strCurrentPWD.ReleaseBuffer();
// IniFile.h
#ifndef __INIFILE_H__
#define __INIFILE_H__
class CIniFile
{
public:
CIniFile();
CIniFile(LPCTSTR szFileName);
virtual ~CIniFile();
public:
// Attributes
void SetFileName(LPCTSTR szFileName);
public:
// Operations
BOOL SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue);
BOOL SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTRlpszKeyValue);
DWORD GetProfileSectionNames(CStringArray& strArray); // 返回section数量
int GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);
DWORD GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue);
BOOL DeleteSection(LPCTSTR lpszSectionName);
BOOL DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);
private:
CString m_szFileName; // .//Config.ini, 如果该文件不存在,则exe第一次试图Write时将创建该文件
UINT m_unMaxSection; // 最多支持的section数(256)
UINT m_unSectionNameMaxSize; // section名称长度,这里设为32(Null-terminated)
void Init();
};
#endif
// IniFile.cpp
#include "IniFile.h"
void CIniFile::Init()
{
m_unMaxSection = 512;
m_unSectionNameMaxSize = 33; // 32位UID串
}
CIniFile::CIniFile()
{
Init();
}
CIniFile::CIniFile(LPCTSTR szFileName)
{
// (1) 绝对路径,需检验路径是否存在
// (2) 以"./"开头,则需检验后续路径是否存在
// (3) 以"../"开头,则涉及相对路径的解析
Init();
// 相对路径
m_szFileName.Format(".//%s", szFileName);
}
CIniFile::~CIniFile()
{
}
void CIniFile::SetFileName(LPCTSTR szFileName)
{
m_szFileName.Format(".//%s", szFileName);
}
DWORD CIniFile::GetProfileSectionNames(CStringArray &strArray)
{
int nAllSectionNamesMaxSize = m_unMaxSection*m_unSectionNameMaxSize+1;
char *pszSectionNames = newchar[nAllSectionNamesMaxSize];
DWORD dwCopied = 0;
dwCopied = ::GetPrivateProfileSectionNames(pszSectionNames, nAllSectionNamesMaxSize, m_szFileName);
strArray.RemoveAll();
char *pSection = pszSectionNames;
do
{
CString szSection(pSection);
if (szSection.GetLength() < 1)
{
delete[] pszSectionNames;
return dwCopied;
}
strArray.Add(szSection);
pSection = pSection + szSection.GetLength() + 1; // next section name
} while (pSection && pSection<pszSectionNames+nAllSectionNamesMaxSize);
delete[] pszSectionNames;
return dwCopied;
}
DWORD CIniFile::GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue)
{
DWORD dwCopied = 0;
dwCopied = ::GetPrivateProfileString(lpszSectionName, lpszKeyName, "",
szKeyValue.GetBuffer(MAX_PATH), MAX_PATH, m_szFileName);
szKeyValue.ReleaseBuffer();
return dwCopied;
}
int CIniFile::GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)
{
int nKeyValue = ::GetPrivateProfileInt(lpszSectionName, lpszKeyName, 0, m_szFileName);
return nKeyValue;
}
BOOL CIniFile::SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName,LPCTSTR lpszKeyValue)
{
return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, lpszKeyValue, m_szFileName);
}
BOOL CIniFile::SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, intnKeyValue)
{
CString szKeyValue;
szKeyValue.Format("%d", nKeyValue);
return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, szKeyValue, m_szFileName);
}
BOOL CIniFile::DeleteSection(LPCTSTR lpszSectionName)
{
return ::WritePrivateProfileSection(lpszSectionName, NULL, m_szFileName);
}
BOOL CIniFile::DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)
{
return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, NULL, m_szFileName);
}
一.将信息写入.INI文件中.
1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
其中各参数的意义:
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中.
CString strName,strTemp;
int nAge;
strName="张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此时c:\stud\student.ini文件中的内容如下:
[StudentInfo]
Name=张三
3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.Format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");
二.将信息从INI文件中读入程序中的变量.
1.所用的WINAPI函数原型为:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
3.读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:
1.写入:
CString strTemp,strTempA;
int i;
int nCount=6;
file://共有6个文件名需要保存
for(i=0;i {strTemp.Format("%d",i);
strTempA=文件名;
file://文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c:\\usefile\\usefile.ini");
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
file://将文件总数写入,以便读出.
2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
for(i=0;i {strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");
file://使用strTempA中的内容.
}
补充四点:
1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
2.文件名的路径中必须为 \\ ,因为在VC++中, \\ 才表示一个 \ .
3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\\student.ini".
4.从网页中粘贴源代码时,最好先粘贴至记事本中,再往VC中粘贴,否则易造成编译错误,开始时我也十分不解,好好的代码怎么就不对呢?后来才找到这个方法.还有一些代码中使用了全角字符如:<,\等,也会
造成编译错误.
ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息。ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Key可以赋相应的值。读写ini文件实际上就是读写某个的Section中相应的Key的值,而这只要借助几个函数即可完成。
一、向ini文件中写入信息的函数
1. 把信息写入系统的win.ini文件
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
2. 把信息写入自己定义的.ini文件
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
LPCTSTR lpString, // 同上
LPCTSTR lpFileName // 要写入的文件的文件名。若该ini文件与程序在同一个目录下,也可使用相对
//路径,否则需要给出绝度路径。
)
如:
::WriteProfileString("Test","id","xym");
//在win.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
::WritePrivateProfileString("Test","id","xym","d://vc//Ex1//ex1.ini");
//在Ex1目录下的ex1.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
//若Ex1.ini文件与读写该文件的程序在同一个目录下,则上面语句也可写为:
::WritePrivateProfileString("Test","id","xym",".//ex1.ini");
需要注意的是,C系列的语言中,转义字符'//'表示反斜线'/'。另外,当使用相对路径时,//前的.号不能丢掉了。
二、从ini文件中读取数据的函数
1、从系统的win.ini文件中读取信息
(1) 读取字符串
DWORD GetProfileString(
LPCTSTR lpAppName, // 节名
LPCTSTR lpKeyName, // 键名,读取该键的值
LPCTSTR lpDefault, // 若指定的键不存在,该值作为读取的默认值
LPTSTR lpReturnedString, // 一个指向缓冲区的指针,接收读取的字符串
DWORD nSize // 指定lpReturnedString指向的缓冲区的大小
)
如:
CString str;
::GetProfileString("Test","id","Error",str.GetBuffer(20),20);
(2) 读取整数
UINT GetProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault // 若指定的键名不存在,该值作为读取的默认值
)
如使用以下语句写入了年龄信息:
::WriteProfileString("Test","age","25");
//在win.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetProfileInt("Test","age",0);
2、从自己的ini文件中读取信息
(1) 读取字符串
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // 同1(1)
LPCTSTR lpKeyName, // 同1(1)
LPCTSTR lpDefault, // 同1(1)
LPTSTR lpReturnedString, // 同1(1)
DWORD nSize, // 同1(1)
LPCTSTR lpFileName // 读取信息的文件名。若该ini文件与程序在同一个目录下,也可使用相
//对路径,否则需要给出绝度路径。
)
如:
CString str;
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,".//ex1.ini");
或:
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,"d://vc//Ex1//ex1.ini");
(2) 读取整数
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault, // 若指定的键名不存在,该值作为读取的默认值
LPCTSTR lpFileName // 同上
)
如使用以下语句写入了年龄信息:
::WritePrivateProfileString("Test","age","25",".//ex1.ini");
//在ex1.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetPrivateProfileInt("Test","age",0,".//ex1.ini");
三、 删除键值或节
回顾一下WriteProfileString函数的说明
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
由此可见,要删除某个节,只需要将WriteProfileString第二个参数设为NULL即可。而要删除某个键,则只需要将该函数的第三个参数设为 NULL即可。这是删除系统的win.ini中的节或键,类似的,要删除自己定义的ini文件中的节或键,也可做相同的操作。
如:
::WriteProfileString("Test",NULL,NULL); //删除win.ini中的Test节
::WriteProfileString("Test","id",NULL); //删除win.ini中的id键
::WritePrivateProfileString("Test",NULL,NULL,".//ex1.ini"); //删除ex1.ini中的Test节
::WritePrivateProfileString("Test","id",NULL,".//ex1.ini"); //删除ex1.ini中的id键
四、如何判断一个ini文件中有多少个节
要判断一个ini文件中有多少个节,最简单的办法就是将所有的节名都找出来,然后统计节名的个数。而要将所有的节名找出来,使用GetPrivateProfileSectionNames函数就可以了,其原型如下:
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // 指向一个缓冲区,用来保存返回的所有节名
DWORD nSize, // 参数lpszReturnBuffer的大小
LPCTSTR lpFileName // 文件名,若该ini文件与程序在同一个目录下,
//也可使用相对路径,否则需要给出绝度路径
)
下面的是用来统计一个ini文件中共有多少个节的函数,当然,如果需要同时找到每个节中的各个键及其值,根据找到节名就可以很容易的得到了。
/*统计共有多少个节
节名的分离方法:若chSectionNames数组的第一字符是'/0'字符,则表明
有0个节。否则,从chSectionNames数组的第一个字符开始,顺序往后找,
直到找到一个'/0'字符,若该字符的后继字符不是 '/0'字符,则表明前
面的字符组成一个节名。若连续找到两个'/0'字符,则统计结束*/
int CTestDlg::CalcCount(void)
{
TCHAR chSectionNames[2048]={0}; //所有节名组成的字符数组
char * pSectionName; //保存找到的某个节名字符串的首地址
int i; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
int j=0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
int count=0; //统计节的个数
//CString name;
//char id[20];
::GetPrivateProfileSectionNames(chSectionNames,2048,".//ex1.ini");
for(i=0;i<2048;i++,j++)
{
if(chSectionNames[0]=='/0')
break; //如果第一个字符就是0,则说明ini中一个节也没有
if(chSectionNames[i]=='/0')
{
pSectionName=&chSectionNames[i-j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,
//就是一个节名的首地址
j=-1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
//赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
//的一部分
/*::GetPrivateProfileString(pSectionName,"id","Error",id,20,".//ex1.ini");
name.Format("%s",id);*/
//在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
AfxMessageBox(pSectionName); //把找到的显示出来
if(chSectionNames[i+1]==0)
{
break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
}
}
}
return count;
}
- MFC文件读写之ini文件
- MFC文件读写之ini文件
- MFC文件读写之ini文件
- MFC文件读写之ini文件
- MFC INI文件读写
- MFC INI文件读写
- MFC INI文件读写
- MFC 读写Ini文件
- 【MFC】VC读写ini文件
- MFC读写ini文件方法
- wince 读写 ini 文件 操作 MFC
- MFC 读写配置文件——ini文件
- MFC创建及读写ini文件操作;
- WIN32、MFC、VC、INI文件读写API
- Qt开发之读写ini文件
- QT 学习之------ 读写ini文件
- C#读写INI文件
- INI文件的读写
- Mycat(3)临时解决utf8mb4编码问题
- opencv人脸识别
- 使用Python批量转换SVG文件为PNG或PDF文件
- 公共方法
- 刘强东:企业家千万别算小钱 我不研究马云
- MFC文件读写之ini文件
- MCU程序设计之STM8S的optionbytes
- HttpServlet注入Spring管理的bean的方法
- reading schema error: null
- 求线段交点
- 转载)爱加密亮相2015广州国际游博会,弘扬安全正能量
- ViewHolder工具类实现
- fragment(8)根据id查找fragment
- 【Android SDK程序逆向分析与破解系列】之三:Android可执行程序DEX分析(二)