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

            路漫漫,長(zhǎng)修遠(yuǎn),我們不能沒(méi)有錢(qián)
            隨筆 - 173, 文章 - 0, 評(píng)論 - 257, 引用 - 0
            數(shù)據(jù)加載中……

            [C++] 使用ifstream.seekg犯的錯(cuò)誤

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

            我要記得,每一個(gè)指針在申明d時(shí)候就必須要初始化啊初始化,調(diào)試了1個(gè)工作日.

            讀日志文件的一個(gè)類(lèi)

            //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);?//將文件指針指向最開(kāi)始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)?)?{?//取消對(duì)注釋和無(wú)用數(shù)據(jù)的解析,
            ?64???????continue;
            ?65?????}
            ?66?
            ?67?????iLen?=?strlen(szBuff);
            ?68?????if?(szBuff[iLen-1]?==?0x0D)?//將每一行配置的換行符后數(shù)據(jù)去掉
            ?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則做好標(biāo)記
            ?81?????????bInSelfSection?=?1;
            ?82?????????continue;
            ?83???????}
            ?84?????}
            ?85?
            ?86?????if?(!bInSelfSection)??//如果沒(méi)有讀到需要的section則繼續(xù)找
            ?87???????continue;
            ?88?
            ?89?????if?(pStrValue?==?NULL){
            ?90???????pStrValue?=?strchr(szBuff,?'=');??//查找'='的位置,沒(méi)有找到則讀下一條
            ?91?????}
            ?92?
            ?93?????if?(pStrValue?==?NULL){
            ?94???????continue;
            ?95?????}
            ?96?????*pStrValue?=?0;?//將'='變?yōu)?0'來(lái)分割value和key
            ?97?????pStrValue++;
            ?98?????if?(*pStrValue?==?0){//如果沒(méi)有讀到value則繼續(xù)讀下一條
            ?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?//測(cè)試程序
            ?2?
            ?3?#include?"../include/IniFile.hpp"
            ?4?
            ?5?int?main(){
            ?6???printf("開(kāi)始分析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文件格式

            #為注釋,處理時(shí)自動(dòng)去掉前后空格
            #######################################################################
            # 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 閱讀(10338) 評(píng)論(8)  編輯 收藏 引用 所屬分類(lèi): GCC/G++

            評(píng)論

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

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

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

            少一個(gè)pchar.h
            2006-05-24 19:03 | xds2000

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

            已經(jīng)添加了 pchar.h 主要是幾個(gè)字符串處理函數(shù)
            2006-05-25 12:45 | Khan's Notebook

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

            lz用的是什么編譯器?
            seekg(ios_base::beg)
            相當(dāng)于
            seekg(ios_base::beg, ios_base::beg)
            在vc7.1跟gcc3.42里面都是移動(dòng)到文件頭的啊,因?yàn)樵谒鼈兝锩鎖os_base::beg的值其實(shí)是0
            2006-05-29 20:24 | 3×7=51

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

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

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

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

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

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

            # re: [C++] 使用ifstream.seekg犯的錯(cuò)誤   回復(fù)  更多評(píng)論   

            mFile.seekg(ios_base::beg);
            我實(shí)驗(yàn)的結(jié)果是,后者會(huì)定位到文件頭后面一個(gè)字符,也就是說(shuō),第一個(gè)字符被吃掉了。

            因?yàn)橐话愕膇os_base::beg都是定義的0x01,然后你剛剛的寫(xiě)法就正好定位到第一個(gè)字節(jié)去了~不是被庫(kù)吞吃了哦~
            2009-11-03 13:47 | Kouga
            久久久青草青青国产亚洲免观| 秋霞久久国产精品电影院| 热久久国产欧美一区二区精品| 精品国产青草久久久久福利 | 久久久91精品国产一区二区三区 | 93精91精品国产综合久久香蕉| 99精品国产在热久久无毒不卡| 色偷偷888欧美精品久久久| 亚洲国产精品久久久久婷婷老年| 色婷婷狠狠久久综合五月| 伊人久久大香线蕉亚洲五月天| 久久久久99精品成人片欧美| 国内精品久久久久久野外| 欧美亚洲国产精品久久久久| 99久久婷婷国产综合亚洲| 久久97久久97精品免视看 | 精品少妇人妻av无码久久| 嫩草影院久久国产精品| 狠狠色婷婷久久综合频道日韩 | 精品国产乱码久久久久久浪潮| 无码人妻久久一区二区三区蜜桃| 色婷婷综合久久久久中文一区二区 | 久久99久久99精品免视看动漫| 亚洲国产精品久久久久网站| 久久久久久精品免费看SSS| 国产精品久久久久久久久久免费| 亚洲成色www久久网站夜月| 久久久久人妻一区精品 | 亚洲欧美成人综合久久久| 看全色黄大色大片免费久久久| 中文字幕久久欲求不满| 久久久久无码精品国产| 无码人妻久久一区二区三区免费丨| 久久伊人五月天论坛| 久久99久久成人免费播放| 97精品国产97久久久久久免费| 好属妞这里只有精品久久| 精品久久8x国产免费观看| 久久久久成人精品无码中文字幕 | 色综合久久无码中文字幕| 亚洲精品无码专区久久同性男|