17 18 實現乙個讀取INI檔案的類

2021-08-13 19:22:07 字數 3411 閱讀 7732

開發環境: vs2013, win10

經過很多次修改,和對一些錯誤的學習,才搞定了這麼乙個類:

比如說lpctstr的賦值,new和delete要成對出現,字元陣列和字串的庫函式去取代自己寫字串處理函式等, wcsncpy_s用法,string類的賦值運算子等。

標頭檔案:

/*

對文字的處理要求:'='號之前強制為整數,且只允許開頭後者結尾存在空格(處理後丟棄空格),不允許存在非數字字元,否則將丟棄該鍵值對

=號右邊的value原樣儲存,包括空格的存在.

對於讀取多個ini檔案,只要修改一下,就是在loadfile函式中不要對m_map進行clear

已通過的測試樣例:

1 = abdfadfadfd

*****=

aaaaaa

vvvv

a 123 + 234

555=

zzz=

=dd=123

=567=

*****===

a=11234adfdd

*/#pragma once

#include "stdafx.h"

#include#include#include#include#includeclass queryini

~queryini(){}

public:

/*** @brief 初始化成員變數指標

* @return void

*/ static void init();

/*** @brief delete成員變數指標

* @return void

*/ static void uninit();

public:

/*** @brief 查詢ini檔案中的鍵值,必須先呼叫setpath,傳入路徑,再呼叫getvalue;

* @param[in] const int ikey 要查詢的鍵

* @return 查詢成功返回value字串,否則返回空字串

*/ static std::wstring queryini::getvalue(const int ikey)

; queryini::_getinstance()->_getvalue(ikey, sdes, max_path); //true,得到查詢結果

return sdes;

} /**

* @brief 讀取的檔案路徑

* @param[in] lpctstr pfilepath 讀取的檔案的路徑

* @return 是否成功載入檔案內的資料

*/ static bool loadfile(lpctstr pfilepath);

/*** @brief 清空map中的資料

*/ static void clear();

private:

static queryini* _getinstance();

bool queryini::_removespaceandchecknum(std::wstring &skey);

bool _getvalue(const int ikey, wchar_t* sdes, const int len);

private:

std::mapm_map;

static queryini* ms_pinstance;

};

實現檔案:

#include"stdafx.h"

#include"queryini.h"

#include #include #include #include #include #includeusing namespace std;

queryini* queryini::ms_pinstance = null;

void queryini::init()

}void queryini::uninit()

}bool queryini::loadfile(lpctstr pfilepath)

//這個不一定要哦!

std::wifstream wfilestream(pfilepath, std::ios::binary);

if (wfilestream.is_open())

; wfilestream.read(buf, 2);

wfilestream.clear();

wfilestream.seekg(0, ios::beg);

bool isunicode = (buf[0] == wchar_t(0xff) && buf[1] == wchar_t(0xfe)) || (buf[0] == wchar_t(0xfe) && buf[1] == wchar_t(0xff)) ;

assert(isunicode == true);

wfilestream.imbue(std::locale(wfilestream.getloc(), new std::codecvt_utf16 < wchar_t, 0x10ffff, std::consume_header >));//第二個引數是此平面將讀或寫而不出錯的wchar_t最大值

std::wstring wline;

while (std::getline(wfilestream, wline))

}} wfilestream.close(); //開啟完檔案記得關閉

return true;

} return false;

}void queryini::clear()

queryini* queryini::_getinstance()

return ms_pinstance;

}bool queryini::_removespaceandchecknum(std::wstring &skey)

for (int i = 0; i < skey.size(); i++) }

return true;

}bool queryini::_getvalue(const int ikey, wchar_t* sdes, const int len)

else

}

至於queryini::getvalue(const int ikey)函式為什麼放在標頭檔案而不是在實現檔案中,參考:

windows跨模組記憶體管理的淺薄理解

使用的main函式:

#include "stdafx.h"

#include#include#include#include#include#include #include"queryini.h"

using namespace std;

int _tmain(int argc, _tchar* argv)

從乙個檔案讀取資料

從乙個檔案讀取資料,想必都會。比如舉乙個簡單的例子。下面這個函式,就是從檔案讀取所有資料,一行一行讀取,資料存在des這個陣列中 int getstringfromfile file fp char ch int i 0 int j while feof fp des i 0 return 0 當然...

建立 讀取 存貯INI檔案的方法《一》

內容 ini檔案在系統配置及應用程式引數儲存與設定方面,具有很重要的作用,所以視覺化的程式設計一族,如vb vc vfp delphi等都提供了讀寫ini 檔案的方法,其中delphi中操作ini 檔案,最為簡潔,這是因為delphi6.0提供了乙個tinifile類,使我們可以非常靈活的處理ini...

C 乙個Ini操作類

寫整數 public void writeinteger string section,string ident,int value 讀布林 public bool readbool string section,string ident,bool default catch exception e...