C++读取ini文件的类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类
// Ini.h: interface for the CIni class.
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2000.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//////////////////////////////////////////////////////////////////////// #if !defined(AFX_INI_H__2478E9E2_E904_11D1_93C1_241C08C10000__INCLUDED_)
#define AFX_INI_H__2478E9E2_E904_11D1_93C1_241C08C10000__INCLUDED_ #if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
//
// INI file class
//
// Author:
// Bjarke Viks鴈
// Description:
// Implements helper functions to access
// an .INI configuration file using
// conventional CString operations
// // Ini-file wrapper class
class CIni : public CObject
{
public:
CIni();
CIni( LPCTSTR IniFilename );
virtual ~CIni(); // Methods
public:
// Sets the current Ini-file to use.
RETCODE SetIniFilename(LPCTSTR IniFilename);
//
// Reads an integer from the ini-file.
UINT GetInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault=);
// Reads a boolean value from the ini-file.
BOOL GetBoolean(LPCTSTR lpszSection, LPCTSTR lpszEntry, BOOL bDefault=FALSE);
// Reads a string from the ini-file.
CString GetString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault=NULL);
// Reads a binaryt lump of data from the ini-file.
BOOL GetBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes);
//
// Writes an integer to the ini-file.
BOOL WriteInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
// Writes a boolean value to the ini-file.
BOOL WriteBoolean(LPCTSTR lpszSection, LPCTSTR lpszEntry, BOOL bValue);
// Writes a string to the ini-file.
BOOL WriteString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue);
// Writes a binary lump of data to the ini-file.
BOOL WriteBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes);
// Writes an 'expand string' to the ini-file.
BOOL WriteExpandString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue);
//
// Removes an item from the current ini-file.
BOOL DeleteKey(LPCTSTR lpszSection, LPCTSTR lpszEntry);
// Removes a complete section from the ini-file.
BOOL DeleteSection(LPCTSTR lpszSection); // Variables
protected:
CString m_IniFilename; // The current ini-file used.
}; #endif // !defined(AFX_INI_H__2478E9E2_E904_11D1_93C1_241C08C10000__INCLUDED_)
Ini.cpp
// Ini.cpp: implementation of the CIni class.
// Author: Bjarke Viks鴈
//
// Description:
// Thin wrapper around the Win32 Windows Profile (Ini-file configuration)
// interface.
//
////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "Ini.h" #ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif //////////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////////// CIni::CIni()
{
m_IniFilename.Empty();
} CIni::CIni(LPCTSTR IniFilename)
{
SetIniFilename( IniFilename );
} CIni::~CIni()
{
// Flush .ini file
// (This should perhaps not be here. We risk to slow
// down the system and this would be done at a more appropriate
// time by the OS scheduler anyway)
::WritePrivateProfileString( NULL, NULL, NULL, m_IniFilename );
} //////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////// #define MAX_INI_BUFFER 300 // Defines the maximum number of chars we can
// read from the ini file RETCODE CIni::SetIniFilename(LPCTSTR IniFilename)
{
ASSERT(AfxIsValidString(IniFilename));
m_IniFilename = IniFilename;
if( m_IniFilename.IsEmpty() ) return RET_INVALIDARGS;
return RET_OK;
}; UINT CIni::GetInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
if( m_IniFilename.IsEmpty() ) return ; // error
CString sDefault;
sDefault.Format( _T("%d"), nDefault );
CString s = GetString( lpszSection, lpszEntry, sDefault );
return _ttol( s );
}; CString CIni::GetString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
if( m_IniFilename.IsEmpty() ) return CString();
CString s;
long ret = ::GetPrivateProfileString( lpszSection, lpszEntry, lpszDefault, s.GetBuffer( MAX_INI_BUFFER ), MAX_INI_BUFFER, m_IniFilename );
s.ReleaseBuffer();
if( ret== ) return CString(lpszDefault);
return s;
}; BOOL CIni::GetBoolean(LPCTSTR lpszSection, LPCTSTR lpszEntry, BOOL bDefault)
{
CString s = GetString(lpszSection,lpszEntry);
if( s.IsEmpty() ) return bDefault;
TCHAR c = _totupper( s[] );
switch( c ) {
case _T('Y'): // YES
case _T(''): // 1 (binary)
case _T('O'): // OK
return TRUE;
default:
return FALSE;
};
}; BOOL CIni::GetBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
return FALSE;
}; BOOL CIni::WriteInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
CString s;
s.Format( _T("%d"), nValue );
return WriteString( lpszSection, lpszEntry, s );
}; BOOL CIni::WriteBoolean(LPCTSTR lpszSection, LPCTSTR lpszEntry, BOOL bValue)
{
CString s;
bValue ? s=_T("Y") : s=_T("N");
return WriteString( lpszSection, lpszEntry, s );
}; BOOL CIni::WriteString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
if( m_IniFilename.IsEmpty() ) return RET_NOTINITIALIZED;
return ::WritePrivateProfileString( lpszSection, lpszEntry, lpszValue, m_IniFilename );
}; BOOL CIni::WriteBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPBYTE pData, UINT nBytes)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
return FALSE;
}; BOOL CIni::WriteExpandString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
return FALSE;
}; BOOL CIni::DeleteKey(LPCTSTR lpszSection, LPCTSTR lpszEntry)
{
ASSERT(AfxIsValidString(lpszSection));
ASSERT(AfxIsValidString(lpszEntry));
if( m_IniFilename.IsEmpty() ) return RET_NOTINITIALIZED;
return ::WritePrivateProfileString( lpszSection, lpszEntry, NULL, m_IniFilename );
}; BOOL CIni::DeleteSection(LPCTSTR lpszSection)
{
ASSERT(AfxIsValidString(lpszSection));
if( m_IniFilename.IsEmpty() ) return RET_NOTINITIALIZED;
return ::WritePrivateProfileString( lpszSection, NULL, NULL, m_IniFilename );
};
C++读取ini文件的类的更多相关文章
- C#读取ini文件的方法
最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- Ini文件帮助类
.ini文件是什么 .ini 文件是Initialization File的缩写,就是初始化文件.在Windows系统中,其是配置文件所采用的存储格式(主要是system.ini,win.ini,sy ...
- 读写INI文件操作类
详情介绍:http://zh.wikipedia.org/wiki/INI%E6%96%87%E4%BB%B6 示例: 下面是一个虚拟的程序,其INI文件有两个小节,前面的小节是用来设置拥有者的信息, ...
- C# 读取ini文件 百度问问学习文档
C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...
- python中configparser模块读取ini文件
python中configparser模块读取ini文件 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(se ...
- C# 如何实现完整的INI文件读写类
作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...
- java读取ini文件
ini工具类; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import j ...
随机推荐
- Existence and nonexistence results for anisotropic quasilinear elliptic equations
Fragalà, Ilaria; Gazzola, Filippo; Kawohl, Bernd. Existence and nonexistence results for anisotropic ...
- jquery ready 延迟
$.holdReady(true);//延迟 $.holdReady(false);//解延迟 用于下载文件的时候,异步操作 $.holdReady(true); $.getScript(" ...
- 【weiphp微信开发教程】留言板插件开发详解
基于weiphp框架的留言板插件教程: 1.功能分析 传统的留言板应该具有发布留言.查看留言.回复留言.管理留言等功能,本教程开发的是最基本的留言板,仅包含发布留言和查看留言两个功能,根据功能用boo ...
- 安装ipython import path error
sudo pip install ipython --upgrade # 升级 版本兼容性问题,升级到最新版本的ipython 后,会报错 from pickleshare import Pickle ...
- solr学习之添加文档
一.开篇语 其实Solr就是一个你可以通过他来查询文档的东西,他整个都是基于Document的,那么这些Document从何而来列? 当然是我们给他,而这些来源就包括了:数据库文件,XML,Json ...
- 01- 使用brew 安装ant -学习笔记(一)
1.卸载Mac OS下brew工具:ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mast ...
- stat 查看文件修改时间
Ø 访问时间(accesstime):读取一次文件的内容,该时间便会更新 Ø 修改时间(modifytime):对文件内容修改一次便会更新该时间. Ø 改变时间(changetime):更改文件 ...
- linux secureCRT utf-8编码显示
secureCRT 会话选项-终端-外观-字符编码: 下拉选择UTF-8 关闭当前secureCRT,另开一个新的让设置生效,显示正常.
- 如何正确地使用Python的属性和描述符
关于@property装饰器 在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问. 那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联 ...
- c语言实现词频统计
需求: 1.设计一个词频统计软件,统计给定英文文章的单词频率. 2.文章中包含的标点不计入统计. 3.将统计结果以从大到小的排序方式输出. 设计: 1.因为是跨专业0.0···并不会c++和java, ...