• <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>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            [C++] 使用ifstream.seekg犯的錯誤

            使用ifstream的時候,可以用seekg進行重定位,但有一個需要注意的地方。
            如果想重定位到文件頭,應該用:
            mFile.seekg(0, ios_base::beg);
            而不是
            mFile.seekg(ios_base::beg);
            我實驗的結果是,后者會定位到文件頭后面一個字符,也就是說,第一個字符被吃掉了。

            我要記得,每一個指針在申明d時候就必須要初始化啊初始化,調試了1個工作日.

            讀日志文件的一個類

            //pchar.hpp
            ?1?#include?<string.h>
            ?2?
            ?3?void?trimLeft(char?*?p_char){
            ?4???char?szBuff[1024];
            ?5???memset(szBuff,?0,?sizeof(szBuff));
            ?6???char?*pBuff?=?p_char;
            ?7???while(?*pBuff?==?'?'){
            ?8?????++?pBuff;
            ?9???}
            10?
            11???strncpy(szBuff,?pBuff,?sizeof(szBuff)?-1);
            12???strncpy(p_char,?szBuff,?strlen(szBuff));
            13???p_char[strlen(szBuff)]?=?0x0;
            14?}
            15?
            16?
            17?void?trimRight(char?*?p_char){
            18???for(int?i?=?strlen(p_char)?-?1;?i?>=?0;?i--?){
            19?????if(?*(p_char?+?i)?==?'?')?{
            20???????*(p_char?+?i?)?=?'\0';
            21?????}else?break;
            22???}
            23?}
            24?
            25?void?trim(char?*?p_char){
            26???trimLeft(p_char);
            27???trimRight(p_char);
            28?}
            29?
            30?



            ?1?//IniFile.hpp
            ?2?
            ?3?#include?<unistd.h>
            ?4?#include?<stdio.h>
            ?5?#include?<stdlib.h>
            ?6?#include?<string>
            ?7?#include?<fstream>
            ?8?
            ?9?using?namespace?std;
            10?
            11?#ifndef?INIFILE_HPP_
            12?#define?INIFILE_HPP_
            13?
            14?
            15?class?IniFile?{
            16?
            17???public:
            18?????IniFile(){?m_bOpened=?false;}
            19?
            20?????IniFile(const?char?*?p_szFileName);
            21?????~IniFile();
            22?
            23???public:
            24?????void?setFileName(const?char?*?p_szFileName);
            25?????int?openIni();
            26?????int?getPchar?(const?char?*p_szSection,?const?char?*p_szKey,?char?*p_szDefaultValue);
            27?????int?getInt???(const?char?*p_szSection,?const?char?*p_szKey,?int&?p_iDefaultValue);
            28?????int?getBool??(const?char?*p_szSection,?const?char?*p_szKey,?int&?p_iDefaultValue);
            29?????int?getString(string?p_strSection,?string?p_strKey,?string&?p_strDefaultValue);
            30?
            31???protected:
            32?
            33?
            34???private:
            35?????ifstream?inFile;
            36?????char?m_szFileName[256];
            37?????char?m_szSection[32];
            38?????bool?m_bOpened;
            39?};
            40?
            41?#endif?//INIFILE_HPP_
            42?
            43?


            ??1?//IniFile.cpp
            ??2?
            ??3?#include?"../include/IniFile.hpp"
            ??4?#include?"../include/pchar.hpp"
            ??5?
            ??6?
            ??7?IniFile::IniFile(const?char?*?p_szFileName){
            ??8???m_bOpened?=?false;
            ??9???setFileName(p_szFileName);
            ?10?}
            ?11?
            ?12?IniFile::~IniFile(){
            ?13???inFile.close();
            ?14???printf("ini文件句柄釋放!\n");
            ?15?}
            ?16??????????????????????????
            ?17?void?IniFile::setFileName(const?char?*?p_szFileName){
            ?18???memset(?m_szFileName,?0,?sizeof(m_szFileName)?);
            ?19???strncpy(?m_szFileName,?p_szFileName,?sizeof(m_szFileName)-1?);
            ?20?}
            ?21?
            ?22?
            ?23?
            ?24?
            ?25?int?IniFile::openIni(){
            ?26???inFile.open(m_szFileName,?ios::in);
            ?27???if?(!?inFile.good()){
            ?28?????return?-1;
            ?29?????m_bOpened=?false;
            ?30???}
            ?31?
            ?32???m_bOpened=?true;
            ?33???return?0;
            ?34?}
            ?35?
            ?36?
            ?37?int?IniFile::getPchar(const?char?*p_szSection,?const?char?*p_szKey,?char?*p_szDefaultValue){
            ?38???char?szBuff[1024];
            ?39???char?szKey[32]?;
            ?40???char?szDefaultValue[64]?;
            ?41???char?szCurrentSection[32];
            ?42???string?strLine;
            ?43???int?iLen?=?0,?bInSelfSection?=?0?;
            ?44???char?*?pStrValue?=?NULL?;
            ?45????????????????????????????????????????
            ?46???memset(m_szSection,?0,?sizeof(m_szSection)?)?;
            ?47???strncpy(m_szSection,?p_szSection,?sizeof(m_szSection)-1);
            ?48???
            ?49???memset(szKey,?0,?sizeof(szKey));
            ?50???strncpy(szKey,?p_szKey,?sizeof(szKey)?-?1);
            ?51?
            ?52???memset(szDefaultValue,?0,?sizeof(szDefaultValue));
            ?53???strncpy(szDefaultValue,?p_szDefaultValue,?sizeof(szDefaultValue)-1);
            ?54?
            ?55???inFile.seekg(0,?ios_base::beg);?//將文件指針指向最開始d位置
            ?56???while(!inFile.eof()){
            ?57?????getline(inFile,?strLine);
            ?58?
            ?59?????memset(szBuff,?0,?sizeof(szBuff));
            ?60?????snprintf(szBuff,?sizeof(szBuff)-1,?"%s",?strLine.c_str());
            ?61?????trim(szBuff);
            ?62?
            ?63?????if?(?(szBuff[0]?==?'#')?||?(strlen(szBuff)?<?3)?)?{?//取消對注釋和無用數據的解析,
            ?64???????continue;
            ?65?????}
            ?66?
            ?67?????iLen?=?strlen(szBuff);
            ?68?????if?(szBuff[iLen-1]?==?0x0D)?//將每一行配置的換行符后數據去掉
            ?69???????szBuff[iLen-1]?=?0x0;
            ?70?
            ?71?????iLen?=?strlen(szBuff);
            ?72?????if?(szBuff[0]?==?'['?&&?szBuff[iLen-1]?==?']')?{?//判斷是否為Section??[MOSERVER]
            ?73???????if?(bInSelfSection)
            ?74?????????bInSelfSection?=?0;
            ?75?
            ?76???????szBuff[iLen-1]?=?0x0;
            ?77???????memset(szCurrentSection,?0,?sizeof(szCurrentSection));
            ?78???????strcpy(szCurrentSection,?szBuff?+?1);?//取出section名
            ?79?
            ?80???????if?(strcasecmp(m_szSection,?szCurrentSection)?==?0)?{??//如果是自己需要的section則做好標記
            ?81?????????bInSelfSection?=?1;
            ?82?????????continue;
            ?83???????}
            ?84?????}
            ?85?
            ?86?????if?(!bInSelfSection)??//如果沒有讀到需要的section則繼續找
            ?87???????continue;
            ?88?
            ?89?????if?(pStrValue?==?NULL){
            ?90???????pStrValue?=?strchr(szBuff,?'=');??//查找'='的位置,沒有找到則讀下一條
            ?91?????}
            ?92?
            ?93?????if?(pStrValue?==?NULL){
            ?94???????continue;
            ?95?????}
            ?96?????*pStrValue?=?0;?//將'='變為'0'來分割value和key
            ?97?????pStrValue++;
            ?98?????if?(*pStrValue?==?0){//如果沒有讀到value則繼續讀下一條
            ?99???????continue;
            100?????}
            101?????if?(bInSelfSection)?{
            102???????if(strcasecmp(szKey,szBuff)==0){
            103?????????strncpy(p_szDefaultValue,?pStrValue,?64?-?1);
            104?????????return?0;
            105???????}
            106?????}
            107?????pStrValue?=?NULL;
            108???}
            109???return?-1;
            110?}
            111?
            112?
            113?int?IniFile::getString(string?p_strSection,?string?p_strKey,?string&?p_strDefaultValue){
            114???inFile.seekg(0,?ios_base::beg);
            115???char?szDefaultValue[64];
            116???memset(szDefaultValue,?0,?sizeof(szDefaultValue));
            117???getPchar(p_strSection.c_str(),?p_strKey.c_str(),?szDefaultValue);
            118???getPchar("MOSERVER",?"TIMEOUT",?szDefaultValue);
            119???//printf("getchar:?%s\n",?szDefaultValue);//
            120???p_strDefaultValue?=?szDefaultValue;
            121???return?0;
            122?}?
            123?
            124?


            ?1?//測試程序
            ?2?
            ?3?#include?"../include/IniFile.hpp"
            ?4?
            ?5?int?main(){
            ?6???printf("開始分析ini文件!\n");
            ?7???IniFile?ini;
            ?8???char?szPort[64];
            ?9???memset(szPort,?0,?sizeof(szPort));
            10?
            11???ini.setFileName("../config/inifile.ini");
            12???ini.openIni();
            13???ini.getPchar("MOSERVER",?"MAXCONNECTED",?szPort);
            14???printf("ini文件裝載完成!\n");
            15?
            16???string?timeout;
            17???ini.getString("MOSERVER",?"TIMEOUT",?timeout);
            18?
            19???printf("MAXCONNECTED:%s\n",?szPort);
            20???printf("TIMEOUT:%s\n",?timeout.c_str());
            21?
            22???return?0;
            23?}



            inifile.ini文件格式

            #為注釋,處理時自動去掉前后空格
            #######################################################################
            # the configuration for MOServer
            # by Khan.Lau (Lau.Khan#gmail.com)
            # 2006-04-12
            #######################################################################

            [MOSERVER]
            LISTENPORT=8097
            WHITEIP=192.168.1.122;127.0.0.1;
            MAXCONNECTED=50
            MAXCONSINGLEIP=5
            TIMEOUT=60
            DBHOST=127.0.0.1
            DBNAME=smstest
            DBPORT=9055
            DBUID=postgres
            DBPWD=

            posted on 2006-05-24 15:48 Khan 閱讀(10319) 評論(8)  編輯 收藏 引用 所屬分類: GCC/G++

            評論

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            大意了大意了,缺了一個
            public void closeIni(){
            inFile.close();
            printf("ini文件句柄釋放!\n");
            }
            2006-05-24 15:54 | Khan's Notebook

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            少一個pchar.h
            2006-05-24 19:03 | xds2000

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            已經添加了 pchar.h 主要是幾個字符串處理函數
            2006-05-25 12:45 | Khan's Notebook

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            lz用的是什么編譯器?
            seekg(ios_base::beg)
            相當于
            seekg(ios_base::beg, ios_base::beg)
            在vc7.1跟gcc3.42里面都是移動到文件頭的啊,因為在它們里面ios_base::beg的值其實是0
            2006-05-29 20:24 | 3×7=51

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            我用的是gcc 3.42
            2006-05-30 10:33 | Khan's Notebook

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            ……我在mingw里面(使用gcc3.42)里面試怎么是移動到文件頭一個字節……
            2006-05-30 10:48 | 3×7=51

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            我用cygwin ,估計是rp吧......
            2006-05-30 10:50 | Khan's Notebook

            # re: [C++] 使用ifstream.seekg犯的錯誤   回復  更多評論   

            mFile.seekg(ios_base::beg);
            我實驗的結果是,后者會定位到文件頭后面一個字符,也就是說,第一個字符被吃掉了。

            因為一般的ios_base::beg都是定義的0x01,然后你剛剛的寫法就正好定位到第一個字節去了~不是被庫吞吃了哦~
            2009-11-03 13:47 | Kouga
            久久久久女人精品毛片| 国产国产成人久久精品| 久久精品亚洲男人的天堂| av无码久久久久久不卡网站| 亚洲午夜久久久| 久久久久久亚洲精品不卡| 狠狠色综合久久久久尤物| 国产精品美女久久久久AV福利| 国产国产成人精品久久| 999久久久无码国产精品| 国产精品国色综合久久| 东京热TOKYO综合久久精品| 99久久国产综合精品麻豆| 91精品国产乱码久久久久久| AV色综合久久天堂AV色综合在| 精品久久久久久久无码| 精品国产乱码久久久久久郑州公司| 激情伊人五月天久久综合| 精品久久8x国产免费观看| 91麻豆精品国产91久久久久久| 久久精品国产99国产电影网| 国产高清国内精品福利99久久| 日韩欧美亚洲综合久久影院Ds| 久久99国产精品久久99小说| 精品久久亚洲中文无码| 97精品伊人久久大香线蕉app| 日本福利片国产午夜久久| 久久久精品久久久久久| 色天使久久综合网天天| 久久精品国产亚洲av日韩| 久久国产免费观看精品| 日本亚洲色大成网站WWW久久| 久久精品中文字幕一区| 久久国产色AV免费看| 很黄很污的网站久久mimi色| 久久久国产99久久国产一| 久久99精品久久久久久hb无码| 国产国产成人久久精品| 无码AV中文字幕久久专区| 国产高潮久久免费观看| 久久亚洲AV成人无码电影|