博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++读取ini文件的类
阅读量:5125 次
发布时间:2019-06-13

本文共 6832 字,大约阅读时间需要 22 分钟。

取自: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 classclass CIni : public CObject  {public:   CIni();   CIni( LPCTSTR IniFilename );   virtual ~CIni();// Methodspublic:   // 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=0);   // 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);// Variablesprotected:   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_FILEstatic 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 0; // 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==0 ) 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[0] );   switch( c ) {   case _T('Y'): // YES   case _T('1'): // 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 );};

 

posted on
2014-03-23 14:09 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/kernel0815/p/3619006.html

你可能感兴趣的文章
java处理url中的特殊字符%等
查看>>
你的第一个Django程序
查看>>
Tomcat免安装版的环境变量配置以及Eclipse下的Tomcat配置和测试
查看>>
Unity3D性能优化之Draw Call Batching
查看>>
grafana授权公司内部邮箱登录 ldap配置
查看>>
treegrid.bootstrap使用说明
查看>>
[Docker]Docker拉取,上传镜像到Harbor仓库
查看>>
javascript 浏览器类型检测
查看>>
nginx 不带www到www域名的重定向
查看>>
记录:Android中StackOverflow的问题
查看>>
导航,头部,CSS基础
查看>>
[草稿]挂载新硬盘
查看>>
[USACO 2017 Feb Gold] Tutorial
查看>>
关于mysql中GROUP_CONCAT函数的使用
查看>>
OD使用教程20 - 调试篇20
查看>>
Java虚拟机(JVM)默认字符集详解
查看>>
Java Servlet 过滤器与 springmvc 拦截器的区别?
查看>>
(tmp >> 8) & 0xff;
查看>>
linux命令之ifconfig详细解释
查看>>
NAT地址转换
查看>>