操作ini文件

概述

 

在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置、大小、一些用户设置的数据等等,在 Dos 下编程的时候,我们一般自己产生一个文件,由自己把这些数据写到文件中,然后在下一次执行的时候再读出来使用。在 Win32 编程中当然你也可以这样干,但 Windows 已经为我们提供了两种方便的办法,那就是使用注册表或者 ini 文件(Profile)来保存少量数据。本文中先介绍一下 .ini 文件的使用。

 

ini 文件是文本文件,中间的数据格式一般为:

[Section1 Name]

KeyName1=value1

KeyName2=value2

...

 

[Section2 Name]

KeyName1=value1

KeyName2=value2

 

ini 文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=valueWin32 ini 文件操作的 api 中,有一部分是对 win.ini 操作的,有一部分是对用户自定义的 ini 文件操作的。Win.in system.ini Windows的两个非常重要的初始化文件,Windows将用户所作的选择以及各种变化的系统信息记录在这两个文件中。System.ini 描述了系统硬件的当前状态,Win.ini 文件则包含了Windows 系统运行环境的当前配置。由于 Win.ini 文件的重要性和常用性,Win32 中有专门对 Win.ini 进行操作的 api,它们是:

 

GetProfileInt - Win.ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:

 

GetProfileInt(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少

);

 

如果 Key 值没有找到的话,返回值是 nDefault 指定的缺省值,如果 Key 中的值是负数,则返回 0,如果 Key 指定的是数字和字符串的混合,则返回数字部分的值,比如说 x=1234abcd,则返回 1234

 

 

GetProfileString - Win.ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:

 

GetProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址

LPTSTR lpReturnedString, // 返回字符串的缓冲区地址

DWORD nSize // 缓冲区的长度

);

 

返回的字符串在缓冲区内,返回的 eax 值是返回的字符串的长度(不包括尾部的0

 

 

GetProfileSection - Win.ini 文件中读出整个 Section 的内容,它的原形是:

 

GetProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPTSTR lpReturnedString, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

);

 

 

WriteProfileSection - 将一个整个 Section 的值 写入 Win.ini 文件的指定 Section 中,它的原形是:

 

WriteProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpString // 要写入的数据的地址

);

 

如果 Win.ini 没有指定的 SectionAPI 会新建立一个并写入数据,如果已经存在,则先删除原来 Seciton 中所有的 Key 值然后写入新的。

 

 

WriteProfileString - 将一个 Key 值写入 Win.ini 文件的指定 Section 中,它的原形是:

 

WriteProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpString // 要写的字符串地址

);

 

如果 Win.ini 没有指定的 SectionAPI 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。

以上的 Api 是对 Win.ini 操作的,当然对于我们来说,用的更多的是在程序运行的目录中建立自己的 ini 文件,如果需要对自己的 ini 文件操作,就要用到另一组 Api,这一组 api 和上面的很象,只要把上面一组的 Profile 换成 PrivateProfile(私有的)就可以了,参数中也相应的多了一个 ini 文件名的参数。例如 GetPrivateProfileIntGetPrivateProfileSectionWritePrivateProfileString 等等, 下面分别介绍:

 

GetPrivateProfileInt - ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:

 

GetPrivateProfileInt(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少

LPCTSTR lpFileName // ini 文件的文件名

);

 

中间参数和返回值的定义和 GetProfileInt 是一样的。

 

 

GetPrivateProfileString - ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:

 

GetPrivateProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址

LPTSTR lpReturnedString, // 返回字符串的缓冲区地址

DWORD nSize // 缓冲区的长度

LPCTSTR lpFileName // ini 文件的文件名

);

 

 

GetPrivateProfileSection - ini 文件中读出整个 Section 的内容,它的原形是:

 

GetPrivateProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPTSTR lpReturnedString, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

LPCTSTR lpFileName // ini 文件的文件名

);

 

这个 api 可以读出整个 section 的内容,当你不知道 section 中有哪些 key 的时候,可以使用这个 api 将整个 section 读出后再处理。

 

 

GetPrivateProfileSectionNames - ini 文件中获得 Section 的名称,它的原形是:

 

GetPrivateProfileSectionNames(

LPTSTR lpszReturnBuffer, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

LPCTSTR lpFileName // ini 文件的文件名

);

 

如果 ini 中有两个 Section: [sec1] [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道 ini 中有哪些 section 的时候可以用这个 api 来获取名称

 

 

WritePrivateProfileSection - 将一个整个 Section 的内容入 ini 文件的指定 Section 中,它的原形是:

 

WritePrivateProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpString // 要写入的数据的地址

LPCTSTR lpFileName // ini 文件的文件名

);

 

 

WritePrivateProfileString - 将一个 Key 值写入 ini 文件的指定 Section 中,它的原形是:

 

WritePrivateProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpString // 要写的字符串地址

LPCTSTR lpFileName // ini 文件的文件名

);

 

如果 ini 中没有指定的 SectionAPI 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。当指定的 ini 也不存在的时候,API 会自动建立一个新的文件,所以使用 ini 的好处是我们不必为了保存少量的数据涉及到文件操作,就连查找文件是否存在的操作都不必要。

 

使用要点:

 

在我们实际使用的时候,用的最多的是 GetPrivateProfileString WritePrivateProfileString,但在对自定义 ini 文件操作的时候要注意的是,如果 lpFileName 指定的文件没有路径的话,Api 会去 Windows 的安装目录去找而不会在当前目录找,但是每次用到 ini 函数要获取当前路径显然太麻烦了,这里有一个变通的办法,你只要在 ini 文件名前面加上 ./ 就可以了,比如说要对本目录下的 user.ini 操作,那么文件名就是 './user.ini' 这样显然比较方便。另外,当你要把一个 Key 清除的时候,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileString。当你要把一个 section 的全部内容清空的时候,也不必把 key 一个个的清除,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileSection

 

 

一个操作ini文件的类:

.h

// IniFile.h: interface for the CIniReader class.

//

//

 

#if !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_)

#define AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include <afxcoll.h>

 

class CIniReader 

{

public:

       // method to set INI file name, if not already specified

       void setINIFileName(CString strINIFile);

      

       // methods to return the lists of section data and section names

       CStringList* getSectionData(CString strSection);

       CStringList* getSectionNames();

      

       // check if the section exists in the file

       BOOL sectionExists(CString strSection);

 

       // updates the key value, if key already exists, else creates a key-value pair

       long setKey(CString strValue, CString strKey, CString strSection);

 

       // give the key value for the specified key of a section

       CString getKeyValue(CString strKey,CString strSection);

      

       // default constructor

       CIniReader()

       {

              m_sectionList = new CStringList();

              m_sectionDataList = new CStringList();

       }

      

       CIniReader(CString strFile)

       {

              m_strFileName = strFile;

              m_sectionList = new CStringList();

              m_sectionDataList = new CStringList();

       }

      

       ~CIniReader()

       {

              delete m_sectionList;

              delete m_sectionDataList;

       }

      

private:

       // lists to keep sections and section data

       CStringList *m_sectionDataList;

       CStringList *m_sectionList;

      

       CString m_strSection;

       long m_lRetValue;

      

       // ini file name

       CString m_strFileName;

};

 

#endif // !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_)

 

.cpp

// IniFile.cpp: implementation of the CIniReader class.

//

//

 

#include "stdafx.h"

//#include "Readini.h"

#include "INI.h"

#include <afxcoll.h>

 

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

 

//

// Construction/Destruction

//

 

 

 

// Used to retrieve a value give the section and key

CString CIniReader::getKeyValue(CString strKey,CString strSection)

{

       char ac_Result[255];

 

       // Get the info from the .ini file     

       m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,

              "",ac_Result, 255, (LPCTSTR)m_strFileName); 

 

       CString strResult(ac_Result);

       return strResult;

}

 

// Used to add or set a key value pair to a section

long CIniReader::setKey(CString strValue, CString strKey, CString strSection)

{

       m_lRetValue = WritePrivateProfileString (strSection, strKey,

                             strValue, m_strFileName);

 

       return m_lRetValue;

}

 

// Used to find out if a given section exists

BOOL CIniReader::sectionExists(CString strSection)

{

       char ac_Result[100];    

       CString csAux;

       // Get the info from the .ini file     

       m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,

              "",ac_Result, 90, (LPCTSTR)m_strFileName);

       // Return if we could retrieve any info from that section

       return (m_lRetValue > 0);

}

 

// Used to retrieve all of the  section names in the ini file

CStringList* CIniReader::getSectionNames()  //returns collection of section names

{

       char ac_Result[2000];

       m_sectionList->RemoveAll();

      

       m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);

      

       CString strSectionName;

       for(int i=0; i<m_lRetValue; i++)

       {

              if(ac_Result[i] != '/0') {

                     strSectionName = strSectionName + ac_Result[i];

              } else {

                     if(strSectionName != "") {

                            m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);

                     }

                     strSectionName = "";

              }

       }

 

       return m_sectionList;

}

 

// Used to retrieve all key/value pairs of a given section. 

CStringList* CIniReader::getSectionData(CString strSection) 

{

       char ac_Result[2000];  //change size depending on needs

       m_sectionDataList->RemoveAll();

       m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);

 

       CString strSectionData;

       for(int i=0; i<m_lRetValue; i++)

       {

              if(ac_Result[i] != '/0') {

                     strSectionData = strSectionData + ac_Result[i];

              } else {

                     if(strSectionData != "") {

                            m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);

                     }

                     strSectionData = "";

              }

       }

 

       return m_sectionDataList;

}

 

void CIniReader::setINIFileName(CString strINIFile)

{

       m_strFileName = strINIFile;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值