VC操作INI文件
来源:互联网 发布:fuse源码 编辑:程序博客网 时间:2024/05/18 00:52
INI文件简介
在我们写程序时,总有一些配置信息需要保存下来,以便在下一次启动程序完成初始化,这实际上是一种类持久化。将一些信息写入INI文件(initialization file)中,可完成简单的持久化支持。
Windows提供了API接口用于操作INI文件,其支持的INI文件格式一般如下:
===============================
[Section1]
Key11=value11
Key12=value12
[Section2]
Key21=value21
Key22=value22
...
[SectionN]
KeyN1=valueN1
KeyN2=valueN2
===============================
一般一个INI文件可有N个节,每节可有n个键名及值对应,每个键名及其值以等式形式占一行。
一般键的名称可任取,不过建议用有意义的字符及词构成。值一般可为整数和字符串,其它类型要进行转换。
常见的系统配置文件:
C:/boot.ini
C:/WINDOWS/win.ini
C:/WINDOWS/system.ini
C:/WINDOWS/desktop.ini
C:/WINDOWS/Resources/Themes/Windows Classic.theme
注意,字符串存贮在INI文件中时没有引号;key和value之间的等号前后不容空格;注释以分号“;”开头。
VC中操作INI文件的API
(1)操作系统配置文件Win.ini的函数:
函数名
功能
GetProfileSection
读取win.ini中指定节lpAppName中所有键名及其值。lpReturnedString字符串形式如下:
Key1=Value1/0Key2=Value2/0…KeyN=ValueN/0/0
GetProfileString
读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。
GetProfileInt
读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的整数值。
WriteProfileSection
写(替换)win.ini中指定节lpAppName中的键值。
lpString字符串形式同GetProfileSection中的lpReturnedString。
WriteProfileString
写(替换)win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。
(2)操作用户自定义配置文件(PrivateProfile.ini)的函数:
函数名
功能
GetPrivateProfileSectionNames
读取lpFileName指定的配置文件中所有的节名。lpszReturnBuffer字符串形式如下:
Section1/0Section2/0…SectionN/0/0
GetPrivateProfileSection
同GetProfileSection。
GetPrivateProfileString
同GetProfileString。
GetPrivateProfileInt
同GetProfileInt
GetPrivateProfileStruct
须同WritePrivateProfileStruct配套使用。
WritePrivateProfileSection
同WriteProfileSection
WritePrivateProfileString
同WriteProfileString
WritePrivateProfileStruct
不常用。
注意:
(1)使用得最频繁的是 GetPrivateProfileString和 WritePrivateProfileString,没有WriteProfileInt/WritePrivateProfileInt函数。
(2)Get系列读取节键值,如果文件路径有误或节键名不对则返回设定的默认值。
(3)访存自定义配置文件时,文件路径lpFileName必须完整,文件名前面的各级目录必须存在。如果lpFileName文件路径不存在,则函数返回FALSE,GetLastError() =ERROR_PATH_NOT_FOUND。如果路径正确,但是文件不存在,则该函数将先创建该文件。如果路径及文件存在,则在现有ini文件基础上进行读写。
如果 lpFileName只指定文件名而没有路径的话,调用API将会去 Windows 的安装目录去查找而不会在当前目录查找。
(4)要对调用API的模块(exe)所在目录下进行配置文件操作,可使用形如“.//config.ini”的相对路径,注意转义符。
(5)调用WritePrivateProfileSection,若参数三 lpString为NULL,则可将对应section的全部内容清空;调用WritePrivateProfileString,若参数三 lpString为NULL,则可将对应key删除。
跨平台配置文件
INI文件本质是对文件和字符串的处理,因此在跨平台项目中的配置文件可以基于<stdio.h>中的标C文件FILE,然后实现像类似以上对节([Section])、键(Key)和值(Value)的字符串读写功能。
鉴于XML的树形描述层次结构性清晰,现在很多软件都大面积使用XML文件进行配置,如QQ的全局配置文件C:/Program Files/Tencent/QQ/gf-config.xml。java程序的配置文件基本都使用XML格式,C++中并没有操作XML文件的标准库。
在C/C++程序中要使用XML做为配置文件,涉及到XML的解析。Windows平台可使用MsXml对XML进行解析,参考《MsXml创建和解析XML示例》,跨平台可以考虑自己实现,或使用C++ BOOST正则表达式,或选择Free C or C++ XML Parser Libraries,如XmlParser、TinyXML、CMarkup、libxml等。
CIniFile类
以下提供对Windows操作INI文件的API的简单封装类CIniFile。
- // 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, LPCTSTR lpszKeyValue);
- 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 = new char[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, int nKeyValue)
- {
- 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);
- }
参考:
《在VC++中读写INI文件》
《VC INI文件读写》
《用VC++操作INI文件》
《INI操作类》
《INI文件查看器》
《C++中的XML配置文件编程经验》
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
用Visual C++操作INI文件
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.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<nCount;i++)
{
strTemp.Format("%d",i);
strTempA=文件名;
//文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,"c:\\usefile\\usefile.ini");
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
//将文件总数写入,以便读出.
2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
for(i=0;i<nCount;i++)
{
strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");
//使用strTempA中的内容.
}
补充四点:
1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.
2.文件名的路径中必须为 \\ ,因为在VC++中, \\ 才表示一个 \ .
3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: ".\\student.ini".
//----------------------------------------------------------------------------------
/*
类名:CIni
版本:v2.0
加入高级操作的功能
v1.0
梦小孩于2003年某日
一般操作完成
类描述:
本类可以于.ini文件进行操作
*/
文件 1:
#pragma once
#include "afxTempl.h"
class CIni
{
private:
CString m_strFileName;
public:
CIni(CString strFileName):m_strFileName(strFileName)
{
}
public:
//一般性操作:
BOOL SetFileName(LPCTSTR lpFileName); //设置文件名
CString GetFileName(void); //获得文件名
BOOL SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate=true); //设置键值,bCreate是指段名及键名未存在时,是否创建。
CString GetValue(LPCTSTR lpSection, LPCTSTR lpKey); //得到键值.
BOOL DelSection(LPCTSTR strSection); //删除段名
BOOL DelKey(LPCTSTR lpSection, LPCTSTR lpKey); //删除键名
public:
//高级操作:
int GetSections(CStringArray& arrSection); //枚举出全部的段名
int GetKeyValues(CStringArray& arrKey,CStringArray& arrValue,LPCTSTR lpSection); //枚举出一段内的全部键名及值
BOOL DelAllSections();
};
文件 2:
#include "StdAfx.h"
#include "ini.h"
#define MAX_ALLSECTIONS 2048 //全部的段名
#define MAX_SECTION 260 //一个段名长度
#define MAX_ALLKEYS 6000 //全部的键名
#define MAX_KEY 260 //一个键名长度
BOOL CIni::SetFileName(LPCTSTR lpFileName)
{
CFile file;
CFileStatus status;
if(!file.GetStatus(lpFileName,status))
return TRUE;
m_strFileName=lpFileName;
return FALSE;
}
CString CIni::GetFileName(void)
{
return m_strFileName;
}
BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate)
{
TCHAR lpTemp[MAX_PATH] ={0};
//以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错)
//!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。
if (!bCreate)
{
GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName);
if(strcmp(lpTemp,"!*&*none-value*&!*")==0)
return TRUE;
}
if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName))
return FALSE;
else
return GetLastError();
}
CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey)
{
DWORD dValue;
TCHAR lpValue[MAX_PATH] ={0};
dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName);
return lpValue;
}
BOOL CIni::DelSection(LPCTSTR lpSection)
{
if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}
BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey)
{
if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}
int CIni::GetSections(CStringArray& arrSection)
{
/*
本函数基础:
GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称
如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道
ini 中有哪些 section 的时候可以用这个 api 来获取名称
*/
int i;
int iPos=0;
int iMaxCount;
TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串
TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。
GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName);
//以下循环,截断到两个连续的0
for(i=0;i<MAX_ALLSECTIONS;i++)
{
if (chSectionNames[i]==0)
if (chSectionNames[i]==chSectionNames[i+1])
break;
}
iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrSection.RemoveAll();//清空原数组
for(i=0;i<iMaxCount;i++)
{
chSection[iPos++]=chSectionNames[i];
if(chSectionNames[i]==0)
{
arrSection.Add(chSection);
memset(chSection,0,MAX_SECTION);
iPos=0;
}
}
return (int)arrSection.GetSize();
}
int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection)
{
/*
本函数基础:
GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名
如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道
获得一个段中的所有键及值可以用这个。
*/
int i;
int iPos=0;
CString strKeyValue;
int iMaxCount;
TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串
TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名
GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName);
for(i=0;i<MAX_ALLKEYS;i++)
{
if (chKeyNames[i]==0)
if (chKeyNames[i]==chKeyNames[i+1])
break;
}
iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrKey.RemoveAll();//清空原数组
arrValue.RemoveAll();
for(i=0;i<iMaxCount;i++)
{
chKey[iPos++]=chKeyNames[i];
if(chKeyNames[i]==0)
{
strKeyValue=chKey;
arrKey.Add(strKeyValue.Left(strKeyValue.Find("=")));
arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1));
memset(chKey,0,MAX_KEY);
iPos=0;
}
}
return (int)arrKey.GetSize();
}
BOOL CIni::DelAllSections()
{
int nSection;
CStringArray arrSection;
nSection=GetSections(arrSection);
for(int i=0;i<nSection;i++)
{
if(DelSection(arrSection[i]))
return GetLastError();
}
return FALSE;
}
使用方法:
CIni ini("c:\\a.ini");
int n;
/*获得值
TRACE("%s",ini.GetValue("段1","键1"));
*/
/*添加值
ini.SetValue("自定义段","键1","值");
ini.SetValue("自定义段2","键1","值",false);
*/
/*枚举全部段名
CStringArray arrSection;
n=ini.GetSections(arrSection);
for(int i=0;i<n;i++)
TRACE("%s\n",arrSection[i]);
*/
/*枚举全部键名及值
CStringArray arrKey,arrValue;
n=ini.GetKeyValues(arrKey,arrValue,"段1");
for(int i=0;i<n;i++)
TRACE("键:%s\n值:%s\n",arrKey[i],arrValue[i]);
*/
/*删除键值
ini.DelKey("段1","键1");
*/
/*删除段
ini.DelSection("段1");
*/
/*删除全部
ini.DelAllSections();
*/
操作配置文件ini
1.基础知识
INI文件(Initialization file ,又称为初始化文件)是用来保存应用程序设置和选项的一种特殊的ASCII文件,以“.ini”作为文件扩展名,也被称做配置文件或概要文件(Profile)。除了各个应用程序可以拥有自己私有的初始化文件外,Windows系统还提供有一个系统的初始化文件Win.ini,并由此对当前的Windows系统进行配置,同时也可以在其内记录系统内其他应用程序在运行时的选项。
通常为应用程序所私有的初始化文件比较小,这样可以减少程序在初始化时所读取的信息量,从而提高程序的启动速度。而系统初始化文件Win.ini由于除了记录有关系统的大量信息外,还存储着许多其他应用软件的初始化数据,因此其通常比较庞大,访问的数据量要远比私有的配置文件大得多。如没有必要,一般不建议对Win.ini文件进行操作,但如果待存取的信息涉及到Windows系统环境或是其他应用程序时, 就必须对Win.ini进行读写访问,并在访问的同时发送WM_WININICHANGE消息给所有的顶层窗口,通知其他进程系统初始化文件已被更改。
配置文件里的信息之所以能为系统和众多不同类型的应用程序读取并识别,是由于其内部对数据的存取采用了预先约定的“项-值对(Entry-value pairs)”存储结构, 并对待存取的数据分门别类地进行存储。下面是系统目录下Win.ini文件的部分内容:
[windows]
load=
run=
NullPort=None
[Desktop]
WallpaperStyle=2
Pattern=(无)
在此,配置文件将信息分为若干“节”,节标题放在方括号中,如“[Desktop]”就是Desktop节,在每一个节中包含了一些与之相关的“项”,并通过等号对其进行赋值。一般形式如下:
[SECTION]
ENTRY=VALUE
在初始化文件中,VALUE值只能有两种数据类型:数值和字符串。Windows分别为这两种数据类型提供了两套API函数对初始化文件进行数据读取,在写入初始化文件时则只支持对字符串的写入,数值等类型必须先进行数据类型的转换,然后才能写入到初始化文件。私有初始化文件的访问 对私有初始化文件的数据存取是由GetPrivateProfileInt()、GetPrivateProfileString()和WritePrivateProfileString()等三个API函数来完成的。其函数说明如下:
UINT GetPrivateProfileInt(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
INT nDefault, // 在项名没有找到时返回的缺省值
LPCTSTR lpFileName // 初始化文件名地址
);
DWORD GetPrivateProfileString(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
LPCTSTR lpDefault, // 缺省字符串
LPTSTR lpReturnedString, // 存放字符串的缓冲区地址
DWORD nSize, // 缓冲区大小
LPCTSTR lpFileName // 初始化文件名地址
);
BOOL WritePrivateProfileString(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
LPCTSTR lpString, // 要写入的字符串地址
LPCTSTR lpFileName // 初始化文件名地址
);
其中,GetPrivateProfileInt()返回的是初始化文件lpFileName中lpAppName节内lpKeyName项的整数值,如果没有找到该项则返回缺省值nDefault。如果此项目存在,但值不为整数,则返回0。如果某项目的值中含有非数字字符则只返回第一个非数字前的字符,例如对于“Value = 21century”则只返回数值21。初始化文件名lpFileName可以是全路径也可以只是文件名,如果不指定具体路径,Windows系统将在系统目录对文件进行寻找。GetPrivateProfileString()和WritePrivateProfileString()的用法基本与之类似,只是处理对象的数据类型不同。
2 知识应用
私有初始化文件主要用来保存同应用程序当前状态相关的一些信息,当程序退出后,这些信息由于已写入到初始化文件而得以保留,当程序再次运行时,可以通过对此初始化文件各项数据的读取而得知此应用程序在上次运行期间的相关信息。下面这段代码即通过对私有初始化文件的访问而对程序的运行次数和上一次的运行日期进行记录:
CString sPath,sMsg,sTime,sDate;
char buffer[255];
// 获取当前应用程序全路径
GetModuleFileName(NULL, buffer, MAX_PATH);
sPath = CString(buffer);
sPath = sPath.Left(sPath.ReverseFind('\\'));
// 得到初始化文件的全路径
sPath += "\\Sample04.ini";
// 得到程序累计运行次数
UINT Time = GetPrivateProfileInt("PROGRAM", "RUNTIME", 0, sPath);
// 得到上次运行日期
GetPrivateProfileString("DATE", "LAST", "2002-11-1", buffer, 1000, sPath);
// 显示从初始化文件获取到的文件信息
sMsg.Format("本软件共运行过%d次,上次运行日期为%s", Time, CString(buffer));
AfxMessageBox(sMsg);
// 累加运行次数,并保存到初始化文件
Time++;
sTime.Format("%d", Time);
WritePrivateProfileString("PROGRAM", "RUNTIME", sTime, sPath);
// 获取当前日期,并保存到初始化文件
CTime tm = CTime::GetCurrentTime();
sDate.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
WritePrivateProfileString("DATE", "LAST", sDate, sPath);
在程序执行后,初始化文件Sample04.ini的内容为:
[DATE]
LAST =2002-11-12
[PROGRAM]
RUNTIME =1
系统目录下的Win.ini是一种特殊的初始化文件,主要为系统提供初始化服务,在系统启动时将被系统所访问,并根据其所保存的参数值对系统进行配置。Windows专门提供了三个API函数GetProfileInt()、GetProfileString()和WriteProfileString()对Win.ini进行读写访问,其函数用法同访问私有初始化文件的那几个函数非常类似,只是不必再去指定初始化文件名。下面是这三个函数的原型声明:
UINT GetProfileInt(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
INT nDefault // 在项名没有找到时返回的缺省值
);
DWORD GetProfileString(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
LPCTSTR lpDefault, // 缺省字符串地址
LPTSTR lpReturnedString, // 存放字符串的缓存的地址
DWORD nSize // 缓存的大小
);
BOOL WriteProfileString(LPCTSTR lpAppName, // 节名地址
LPCTSTR lpKeyName, // 项名地址
LPCTSTR lpString // 要写入字符串的地址
);
只要对前面对私有初始化文件进行访问的代码稍加改动即可将程序的配置信息添加到Win.ini中,改动后的代码如下:
CString sPath,sMsg,sTime,sDate;
// 得到程序累计运行次数
UINT Time = GetProfileInt("PROGRAM", "RUNTIME", 0);
// 得到上次运行日期
GetProfileString("DATE", "LAST", "2002-11-1", buffer, 1000);
// 显示从初始化文件获取到的文件信息
sMsg.Format("本软件共运行过%d次,上次运行日期为%s", Time, CString(buffer));
AfxMessageBox(sMsg);
// 累加运行次数,并保存到初始化文件
Time++;
sTime.Format("%d", Time);
WriteProfileString("PROGRAM", "RUNTIME", sTime);
// 获取当前日期,并保存到初始化文件
CTime tm = CTime::GetCurrentTime();
sDate.Format("%d-%d-%d", tm.GetYear(), tm.GetMonth(), tm.GetDay());
WriteProfileString("DATE", "LAST", sDate);
由于Win.ini文件是系统初始化文件,在程序没有运行前文件内不含“DATE”和“PROGRAM”等自定义的节以及其下各项,因此在程序第一次执行后,将由WriteProfileString()函数向Win.ini文件末尾创建相关节、项,并完成数据的写入。
- VC++ 操作 ini 文件
- VC操作INI文件
- VC操作INI文件
- VC操作INI文件
- VC操作INI文件
- VC操作ini文件
- VC操作ini文件
- VC操作INI文件
- vc操作ini文件
- VC操作INI文件
- VC操作INI文件
- VC操作INI文件
- 用VC++操作INI文件
- VC INI文件操作(转)
- 用VC操作INI文件
- 用VC操作INI文件
- VC/MFC 操作INI文件
- VC/MFC 操作ini文件
- C语言用结构体指针作函数参数
- java 从零开始,学习笔记之基础入门<抽象类_接口_多态>(十三)
- JAVA系列-设计模式-抽象工厂模式
- 不得不知道的mysql的基本操作
- apue里面的各种函数
- VC操作INI文件
- mysql的系统信息函数
- 纯vc sdk实现http post 方式上传数据到web服务器
- Windows安装ADB驱动
- 面试题--”根据上排给出十个数,在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数“C语言实现
- linspace()使用说明
- 最常用的几个mysql查询
- mysql的其他常用函数
- 贪心nyoj12 喷水装置(二)