作者:Z-ji | 来源:互联网 | 2024-10-24 17:16
本文介绍了一个基于C++标准库实现的INI文件读写操作类。该类在现有网络资源的基础上进行了扩展和优化,增加了获取当前可执行文件路径和宽字节与多字节字符串转换的功能。通过这些增强功能,该类能够更好地适应各种应用场景,提高代码的可移植性和健壮性。具体实现细节请参见`IniFileSTL.h`文件。
本类是在网络上的一个标准ini读写类修改扩展而来,新增了获取当前可执行文件路径,宽字节转多字节功能
IniFileSTL.h:
#ifndef _FISH_INI_FILE_
#define _FISH_INI_FILE_#include
#include namespace fish
{
using namespace std;class IniFileSTL
{
public:explicit IniFileSTL(const string &fileName);~IniFileSTL(void);bool ReadFile(void);string ReadString( const string §ion, const string &key, const string &value );int ReadInt( const string §ion, const string &key, int value );bool WriteString( const string §ion, const string &key, const string &value );bool WriteInt( const string §ion, const string &key, int value );bool RemoveSection( const string §ion );bool RemoveKey( const string §ion, const string &key );bool WriteFile(void);//获取当前可执行文件的路径(不包含EXE文件名)static string GetExeDir();//宽字节转多字节(wchar-char)static char* WcharToChar(const wchar_t* wp);//多字节转宽字节(char-wchar)static wchar_t* CharToWchar(const char* cp);
private:static string Trim( const string &str );static string LTrim( const string &str );static string RTrim( const string &str );
private:string m_fileName;vector m_vctLine;
};}#endif//简单使用例子
//#include
//#include
//#include "IniFile.h"
//
//using namespace fish;
//
//int main(int argc, char **argv)
//{
// IniFile ini("./test.ini");
// cout<// cout<// return 0;
//}
IniFileSTL.cpp&#xff1a;
#include "stdafx.h"
#include "IniFileSTL.h"
#include
#include
#include
namespace fish
{IniFileSTL::IniFileSTL(const string &fileName)
:m_fileName(fileName)
{ReadFile();
}IniFileSTL::~IniFileSTL(void)
{WriteFile();
}//获取当前可执行文件的路径&#xff08;不包含EXE文件名&#xff09;
string IniFileSTL::GetExeDir()
{wchar_t exeFullPath[MAX_PATH]; // Full path string StrPath&#61;" "; GetModuleFileName(NULL, exeFullPath, MAX_PATH); StrPath&#61;(string)WcharToChar(exeFullPath); // Get full path of the file int leng &#61; StrPath.length();int pos &#61; StrPath.find_last_of(&#39;\\&#39;, StrPath.length()); StrPath&#61;StrPath.substr(0, pos); return StrPath.substr(0, pos); // Return the directory without the file name
}//宽字节转多字节(wchar-char)
char* IniFileSTL::WcharToChar(const wchar_t* wp)
{locale old_loc &#61; locale::global(locale(""));size_t len &#61; wcslen(wp)*sizeof(wchar_t);size_t converted&#61;0;char*CStr;CStr&#61;(char*)malloc(len);wcstombs_s(&converted,CStr,len,wp,_TRUNCATE);locale::global(old_loc);return CStr;
}//多字节转宽字节&#xff08;char-wchar&#xff09;
wchar_t* IniFileSTL::CharToWchar(const char* cp)
{locale old_loc &#61; locale::global(locale(""));size_t len &#61; strlen(cp)*sizeof(char);size_t converted&#61;0;wchar_t *WStr;WStr&#61;(wchar_t*)malloc(2*len);mbstowcs_s(&converted,WStr,len,cp,_TRUNCATE);locale::global(old_loc);return WStr;
}bool IniFileSTL::ReadFile( void )
{ifstream in(m_fileName.c_str());bool bopen &#61; in.is_open();string line;while( getline(in,line) ){m_vctLine.push_back(line);}return true;
}string IniFileSTL::ReadString( const string §ion, const string &key, const string &value )
{for( size_t i &#61; 0;i }bool IniFileSTL::WriteString( const string §ion, const string &key, const string &value )
{for( size_t i &#61; 0;i }bool IniFileSTL::RemoveSection( const string §ion )
{for( size_t i &#61; 0;i }bool IniFileSTL::RemoveKey( const string §ion, const string &key )
{for( size_t i &#61; 0;i }bool IniFileSTL::WriteFile( void )
{ofstream out(m_fileName.c_str());for( size_t i &#61; 0; i }int IniFileSTL::ReadInt( const string §ion, const string &key, int value )
{string str &#61; ReadString( section, key, "" );if( "" &#61;&#61; str ){return value;}istrstream in( str.c_str() );int ret &#61; 0;in>>ret;return ret;
}bool IniFileSTL::WriteInt( const string §ion, const string &key, int value )
{ostrstream out;out<}string IniFileSTL::LTrim( const string &str )
{size_t pos &#61; 0;while( pos !&#61; str.size() ){if( &#39; &#39; &#61;&#61; str[pos] ){&#43;&#43;pos;}else{break;}}return str.substr(pos);
}string IniFileSTL::RTrim( const string &str )
{size_t pos &#61; str.size() - 1;while( pos >&#61; 0 ){if(&#39; &#39; &#61;&#61; str[pos]){--pos;}else{break;}}return str.substr( 0, pos &#43; 1 );
}string IniFileSTL::Trim(const string &str)
{return LTrim( RTrim(str) );
}}
简单使用案例&#xff1a;
string str;fish::IniFileSTL ini(fish::IniFileSTL::GetExeDir()&#43;"\\cfglocal.ini");str&#61;ini.ReadString("OccMarkShow","Showflag","0");
参考文件&#xff1a;csdn下载资源