• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            簡(jiǎn)單好用的讀寫ini文件的類

            IniReader.h

            #ifndef INIREADER_H
            #define INIREADER_H
            class CIniReader
            {
            public:
             CIniReader(
            char* szFileName); 
             
            int ReadInteger(char* szSection, char* szKey, int iDefaultValue);
             
            float ReadFloat(char* szSection, char* szKey, float fltDefaultValue);
             
            bool ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue);
             
            char* ReadString(char* szSection, char* szKey, const char* szDefaultValue);
            private:
              
            char m_szFileName[255];
            }
            ;
            #endif//INIREADER_H

            IniReader.cpp

            #include "IniReader.h"
            #include 
            <iostream>
            #include 
            <Windows.h>
            CIniReader::CIniReader(
            char* szFileName)
            {
             memset(m_szFileName, 
            0x00255);
             memcpy(m_szFileName, szFileName, strlen(szFileName));
            }

            int CIniReader::ReadInteger(char* szSection, char* szKey, int iDefaultValue)
            {
             
            int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName); 
             
            return iResult;
            }

            float CIniReader::ReadFloat(char* szSection, char* szKey, float fltDefaultValue)
            {
             
            char szResult[255];
             
            char szDefault[255];
             
            float fltResult;
             sprintf(szDefault, 
            "%f",fltDefaultValue);
             GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 
            255, m_szFileName); 
             fltResult 
            =  atof(szResult);
             
            return fltResult;
            }

            bool CIniReader::ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue)
            {
             
            char szResult[255];
             
            char szDefault[255];
             
            bool bolResult;
             sprintf(szDefault, 
            "%s", bolDefaultValue? "True" : "False");
             GetPrivateProfileString(szSection, szKey, szDefault, szResult, 
            255, m_szFileName); 
             bolResult 
            =  (strcmp(szResult, "True"== 0 || strcmp(szResult, "true"== 0? true : false;
             
            return bolResult;
            }

            char* CIniReader::ReadString(char* szSection, char* szKey, const char* szDefaultValue)
            {
             
            char* szResult = new char[255];
             memset(szResult, 
            0x00255);
             GetPrivateProfileString(szSection,  szKey, szDefaultValue, szResult, 
            255, m_szFileName); 
             
            return szResult;
            }


            IniWriter.h
            #ifndef INIWRITER_H
            #define INIWRITER_H
            class CIniWriter
            {
            public:
             CIniWriter(
            char* szFileName); 
             
            void WriteInteger(char* szSection, char* szKey, int iValue);
             
            void WriteFloat(char* szSection, char* szKey, float fltValue);
             
            void WriteBoolean(char* szSection, char* szKey, bool bolValue);
             
            void WriteString(char* szSection, char* szKey, char* szValue);
            private:
             
            char m_szFileName[255];
            }
            ;
            #endif //INIWRITER_H

            IniWriter.cpp
            #include "IniWriter.h"
            #include 
            <iostream>
            #include 
            <Windows.h> 
            CIniWriter::CIniWriter(
            char* szFileName)
            {
             memset(m_szFileName, 
            0x00255);
             memcpy(m_szFileName, szFileName, strlen(szFileName));
            }

            void CIniWriter::WriteInteger(char* szSection, char* szKey, int iValue)
            {
             
            char szValue[255];
             sprintf(szValue, 
            "%d", iValue);
             WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
            }

            void CIniWriter::WriteFloat(char* szSection, char* szKey, float fltValue)
            {
             
            char szValue[255];
             sprintf(szValue, 
            "%f", fltValue);
             WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
            }

            void CIniWriter::WriteBoolean(char* szSection, char* szKey, bool bolValue)
            {
             
            char szValue[255];
             sprintf(szValue, 
            "%s", bolValue ? "True" : "False");
             WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
            }

            void CIniWriter::WriteString(char* szSection, char* szKey, char* szValue)
            {
             WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
            }


            Main.cpp
            #include "iostream"
            #include 
            "IniWriter.h"
            #include 
            "IniReader.h"
            int main(int argc, char * argv[])
            {
             CIniWriter iniWriter(
            ".\\Logger.ini");
             iniWriter.WriteString(
            "Setting""Name""jianxx");   
             iniWriter.WriteInteger(
            "Setting""Age"27); 
             iniWriter.WriteFloat(
            "Setting""Height"1.82f); 
             iniWriter.WriteBoolean(
            "Setting""Marriage"false);  
             CIniReader iniReader(
            ".\\Logger.ini");
             
            char *szName = iniReader.ReadString("Setting""Name""");   
             
            int iAge = iniReader.ReadInteger("Setting""Age"25); 
             
            float fltHieght = iniReader.ReadFloat("Setting""Height"1.80f); 
             
            bool bMarriage = iniReader.ReadBoolean("Setting""Marriage"true); 
             
             std::cout
            <<"Name:"<<szName<<std::endl
               
            <<"Age:"<<iAge<<std::endl 
               
            <<"Height:"<<fltHieght<<std::endl 
               
            <<"Marriage:"<<bMarriage<<std::endl; 
             delete szName;  
             
            return 1;   
            }

            posted on 2006-01-17 14:09 夢(mèng)在天涯 閱讀(6166) 評(píng)論(10)  編輯 收藏 引用 所屬分類: CPlusPlus 、MFC/QT

            評(píng)論

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-17 14:29 小明

            雖然可以用,但是不是industry-level的,可以改進(jìn)的地方有(不要怪我雞蛋里面挑骨頭啊):

            1.每次都使用WritePrivateProfileString,GetPrivateProfileString,效率不好,對(duì)于大數(shù)據(jù)量問(wèn)題很大,如果是簡(jiǎn)單數(shù)據(jù)無(wú)所謂,這條忽略。

            2.返回void?如果失敗了,調(diào)用者怎么知道?
            void CIniWriter::WriteString(char* szSection, char* szKey, char* szValue)
            {
            WritePrivateProfileString(szSection, szKey, szValue, m_szFileName);
            }

            3.不支持Unicode.如果不需要支持,ignore

            4.可移植性不好(直接使用了WINAPI)。如果不需要跨平臺(tái),ignore
              回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-17 14:51 夢(mèng)在天涯

            恩,高!學(xué)習(xí)中!

            可不可以在每個(gè)section后加一行空行那?

            請(qǐng)高手指點(diǎn)!  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-18 08:52 fiestay

            配置文件還是使用xml吧,現(xiàn)在是xml的天下了  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-18 10:11 夢(mèng)在天涯

            我覺(jué)的也是的,但是我們的頭讓先用ini,不知道使用起來(lái)那個(gè)效率更好些啊!  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-20 11:19 fiestay

            xml比ini更規(guī)范更通用,而且功能也更強(qiáng)大

            至于效率方面,我沒(méi)有做過(guò)具體測(cè)試,但個(gè)人感覺(jué)ini應(yīng)該比xml的要好一些,xml的解析過(guò)程比ini復(fù)雜,但現(xiàn)在的解析引擎性能都很不錯(cuò),性能的問(wèn)題可以忽略。

            xml這么靈活、強(qiáng)大,為什么不用呢,呵呵

            如果你用xml,給你推薦一個(gè)第三方的解析庫(kù),markup,性能很不錯(cuò),可以嘗試一下。  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-01-23 14:30 ken

            樓主說(shuō)的是ini,和xml有什么關(guān)系,就事論事,討厭那種教育人的口吻
            現(xiàn)在談的不是誰(shuí)好誰(shuí)不好的問(wèn)題,談的是具體的技術(shù)  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2006-06-02 10:39 fang350

            真的很不錯(cuò),能把自己的總結(jié)和經(jīng)驗(yàn)?zāi)贸鰜?lái)給大家共享,肯定是高風(fēng)亮節(jié)的人.
            大家暢所欲言也是很好的,值得提倡!  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2009-02-18 20:51 roger

            GetPrivateProfileString 讀出現(xiàn)失敗,為什么?如何解決!  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2010-08-02 19:02 zhetengfengzi

            http://blog.csdn.net/ddddfw888/archive/2010/08/02/5783165.aspx

            你可以實(shí)現(xiàn)一個(gè)跨平臺(tái)的ini操作類,拋棄微軟的相關(guān)API.  回復(fù)  更多評(píng)論   

            # re: 簡(jiǎn)單好用的讀寫ini文件的類 2011-02-27 15:18 Mzf

            還是很不錯(cuò)的,正需要呢,謝謝了。  回復(fù)  更多評(píng)論   

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1808118
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久精品人人做人人爽97| 99久久亚洲综合精品成人| 一97日本道伊人久久综合影院| 久久强奷乱码老熟女网站| 久久亚洲AV无码精品色午夜| 国产亚洲精品美女久久久| 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品国产久精国产一老狼| 思思久久99热只有频精品66| 久久夜色精品国产噜噜亚洲AV| 国产精品免费福利久久| 亚洲?V乱码久久精品蜜桃| 丁香五月网久久综合| 99久久免费国产精品特黄| 精品久久久久久久| 中文字幕无码免费久久| 中文字幕成人精品久久不卡| 狠狠色婷婷久久综合频道日韩 | 国产精品无码久久综合网| 99精品久久精品一区二区| 精品欧美一区二区三区久久久| 久久国产欧美日韩精品| 中文精品久久久久人妻| 精品乱码久久久久久夜夜嗨| 久久91精品国产91久久户| 亚洲精品国产字幕久久不卡| 无码任你躁久久久久久久| 精品久久一区二区三区| 久久国产色AV免费看| 久久精品无码午夜福利理论片| 无码任你躁久久久久久老妇| 久久综合视频网站| 久久99久久无码毛片一区二区 | 久久电影网一区| 国内精品久久久久影院优| 久久国产精品成人影院| 日产精品久久久久久久性色| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 久久人人爽人人爽人人片AV东京热| 亚洲国产日韩欧美久久| 成人综合久久精品色婷婷|