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

            子彈 の VISIONS

            NEVER back down ~~

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks

            // created by ztwaker on 2006-8-24

            // Used for: Read in the whole file

            //

            ?

            #include < iostream >

            #include < fstream >

            #include < string >

            ?

            bool open ( const std :: string & fn , std :: string & data )

            {

            ?????? std :: ifstream file ( fn . c_str (), std :: ios :: in | std :: ios :: binary );

            ?????? if (! file )

            ????????????? return false ;

            ??????

            ?????? file . seekg (0, std :: ios_base :: end );

            ?????? long len = file . tellg ();

            ?????? file . seekg (0, std :: ios_base :: beg );

            ??????

            ?????? char * buffer = new char [ len ];

            ?????? file . read ( buffer , len );

            ?????? data . assign ( buffer , len );

            ?????? delete [] buffer ;

            ??????

            ?????? file . close ();

            ??? return true ;

            }

            ?

            void test()

            {

            ?????? using namespace std ;

            ?????? const string filename = "Cpp1.ncb" ; //"Cpp22.cpp";

            ?????? string s ;

            ?????? if ( open ( filename , s ))

            ????????????? cout << s . size () << endl << s . c_str () << endl ;

            }

            ?

            int main()

            {

            ??? test();

            ??? return 0;

            }

            ?

            /*

            OUTPUT:

            ?

            33792

            Microsoft C/C++ program database 2.00

            .JG

            */

            posted on 2006-08-24 12:22 子彈のVISIONS 閱讀(2124) 評論(6)  編輯 收藏 引用

            Feedback

            # re: [OPPD] 一次性把整個文件讀到std::string 2006-08-24 15:37 周星星
            為了得到一個文件名稱,而使用string不是一個好主意,建議你使用const char*,以降低依賴
            使用string存儲二進制數(shù)據(jù)也是非常危險的,建議你使用vector<char>
            使用long存儲tellg()的返回值?我建議你還是使用標(biāo)準(zhǔn)的,另外你還得考慮大于2G/4G的文件
            開辟一個buffer,把文件內(nèi)容讀到buffer中,再由buffer存到data中,為什么不直接把文件內(nèi)容讀到data中?另外你還得考慮,new是否能成功

            我寫段代碼你幫我看看
            #include <iostream>
            #include <fstream>
            #include <iterator>
            #include <vector>
            using namespace std;

            int main()
            {
            const char* filename = "d:\\file.txt";

            // 和你的功能一樣,把文件內(nèi)容讀到一個容器中
            {
            ifstream file( filename, ios_base::binary|ios_base::in );
            if( !file ) return -1;

            vector<char> data;
            copy( istreambuf_iterator<char>(file), istreambuf_iterator<char>(), back_inserter(data) );
            }

            // 把文件內(nèi)容讀到一個新文件中
            {
            ifstream file1( filename, ios_base::binary|ios_base::in );
            if( !file1 ) return -1;
            ofstream file2( "d:\\file2.txt", ios_base::binary|ios_base::out );
            if( !file2 ) return -1;

            copy( istreambuf_iterator<char>(file1), istreambuf_iterator<char>(), ostreambuf_iterator<char>(file2) );
            }
            }
              回復(fù)  更多評論
              

            # re: [OPPD] 一次性把整個文件讀到std::string 2006-08-25 09:11 子彈
            @周星星

            認(rèn)真讀了星兄的回復(fù),覺得受益非淺。

            你說得對,為了一個文件名而使用string確實不是好主意;另:
            我在編碼的時候只考慮怎么實現(xiàn)而沒有顧及其他的方方面面(比如:健壯性、魯棒性、性能等)——這是我的一個不好的習(xí)慣。
            無論怎么說,都應(yīng)該用std::ios::pos_type而不是long——我是想ftell返回long,就想當(dāng)然用了long……

            "直接把文件內(nèi)容讀到data中" is a good idea .

            ……要走的路還很長……:)

            希望星兄多來指引指引小弟,非常感謝
              回復(fù)  更多評論
              

            # re: [OPPD] 一次性把整個文件讀到std::string 2006-08-28 11:50 子彈
            參考《Effective STL》條款5和條款6,我重寫為以下函數(shù):

            bool readTheWholeFile(const char* fn, std::vector<char>& data)
            {
            std::ifstream file(fn, std::ios_base::in | std::ios_base::binary);
            if ( !file ) return false;
            std::istreambuf_iterator<char> dataBegin(file);
            std::istreambuf_iterator<char> dataEnd;
            data.assign(dataBegin, dataEnd);
            file.close();
            return true;
            }
              回復(fù)  更多評論
              

            # re: [OPPD] 一次性把整個文件讀到std::string 2007-01-05 19:35 哈胖頭
            一行代碼就足夠了。不過不提倡這種方法。

            string s;

            copy(istreambuf_iterator<char>(ifstream("hello.txt").rdbuf()), istreambuf_iterator<char>(), back_inserter(s));  回復(fù)  更多評論
              

            # re: [OPPD] 一次性把整個文件讀到std::string 2008-09-28 11:45 soli
            學(xué)習(xí)了  回復(fù)  更多評論
              

            # re: [OPPD] 一次性把整個文件讀到std::string 2008-09-28 12:19 soli
            @哈胖頭
            std::string 沒有push_back方法,所以back_inserter(s)是不對的

              回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            精品久久久久中文字幕一区| 伊人久久大香线蕉综合影院首页 | 久久高清一级毛片| 久久久精品视频免费观看| 思思久久好好热精品国产| 久久99国产综合精品免费| 久久五月精品中文字幕| 久久久久亚洲AV无码专区体验| 伊人久久免费视频| 日本久久久久亚洲中字幕| 久久人搡人人玩人妻精品首页 | 久久人与动人物a级毛片| 久久久久久综合一区中文字幕| 伊人热热久久原色播放www| 精品精品国产自在久久高清| 久久精品国产99国产精品亚洲| www.久久热| 久久久久人妻精品一区| 狠狠精品久久久无码中文字幕| 91久久精品视频| 久久精品九九亚洲精品天堂| 久久久亚洲欧洲日产国码aⅴ | 久久国产精品国产自线拍免费| 99久久精品免费看国产一区二区三区| 人妻无码精品久久亚瑟影视| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 蜜臀久久99精品久久久久久| 91麻豆精品国产91久久久久久| 久久亚洲私人国产精品vA| 久久久久久精品久久久久| 中文精品久久久久人妻| 亚洲国产精品嫩草影院久久| 久久天天躁狠狠躁夜夜av浪潮| 久久亚洲电影| 精品久久久无码21p发布| 久久久久av无码免费网| 伊人色综合久久天天人手人婷 | 久久久久久久久久久| 国产成人精品综合久久久久| 久久久国产打桩机| 久久精品麻豆日日躁夜夜躁|