• <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)單好用的讀寫(xiě)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)在天涯 閱讀(6154) 評(píng)論(10)  編輯 收藏 引用 所屬分類: CPlusPlusMFC/QT

            評(píng)論

            # re: 簡(jiǎn)單好用的讀寫(xiě)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)單好用的讀寫(xiě)ini文件的類 2006-01-17 14:51 夢(mèng)在天涯

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

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

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

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

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

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

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

            # re: 簡(jiǎn)單好用的讀寫(xiě)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)單好用的讀寫(xiě)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)單好用的讀寫(xiě)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)單好用的讀寫(xiě)ini文件的類 2009-02-18 20:51 roger

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

            # re: 簡(jiǎn)單好用的讀寫(xiě)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)單好用的讀寫(xiě)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

            搜索

            •  

            積分與排名

            • 積分 - 1804314
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            亚洲综合伊人久久大杳蕉| 久久九九亚洲精品| 香蕉久久AⅤ一区二区三区| 久久99精品免费一区二区| 久久综合九色综合久99| 久久亚洲AV无码精品色午夜| 漂亮人妻被中出中文字幕久久| 亚洲伊人久久精品影院| 久久精品国产99国产精偷| 亚洲国产成人精品91久久久 | 久久久久亚洲AV成人网人人网站| 热久久国产精品| 伊人久久大香线蕉无码麻豆| 国产精品久久永久免费| 久久天天躁夜夜躁狠狠| 久久久精品免费国产四虎| 久久久www免费人成精品| 香蕉久久一区二区不卡无毒影院 | 伊人久久大香线焦综合四虎| 伊人久久一区二区三区无码| 国产一久久香蕉国产线看观看| 最新久久免费视频| 91久久精品国产成人久久| 久久综合狠狠综合久久综合88| 久久人人超碰精品CAOPOREN| 久久婷婷五月综合97色一本一本| 欧美与黑人午夜性猛交久久久 | 91精品国产综合久久香蕉 | 久久精品无码一区二区三区免费| 久久久久亚洲av无码专区喷水| 久久综合给合综合久久| 国产精品狼人久久久久影院 | 国内精品久久久久影院网站| 奇米影视7777久久精品| 狠狠综合久久综合88亚洲| 香蕉久久夜色精品国产2020| 久久99精品免费一区二区| 久久久久亚洲AV无码专区网站| 日本免费久久久久久久网站| 久久国产精品久久久| 91精品国产91久久久久久青草|