• <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++博客 首頁(yè) 新隨筆 聯(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) 評(píng)論(6)  編輯 收藏 引用

            Feedback

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

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

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

            // 和你的功能一樣,把文件內(nèi)容讀到一個(gè)容器中
            {
            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)容讀到一個(gè)新文件中
            {
            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ù)  更多評(píng)論
              

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

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

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

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

            ……要走的路還很長(zhǎng)……:)

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

            # re: [OPPD] 一次性把整個(gè)文件讀到std::string 2006-08-28 11:50 子彈
            參考《Effective STL》條款5和條款6,我重寫(xiě)為以下函數(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ù)  更多評(píng)論
              

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

            string s;

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

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

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

              回復(fù)  更多評(píng)論
              

            亚洲欧美精品一区久久中文字幕| 色偷偷888欧美精品久久久| 久久精品国产一区二区| 一本一本久久a久久精品综合麻豆| 77777亚洲午夜久久多喷| 久久ww精品w免费人成| 久久久久国产一区二区| 久久精品免费一区二区| 婷婷综合久久中文字幕| 精品国产乱码久久久久久呢| 久久综合狠狠综合久久激情 | 日日狠狠久久偷偷色综合96蜜桃 | 久久久黄片| 久久天天躁狠狠躁夜夜2020一| 久久久九九有精品国产| 亚洲欧美国产精品专区久久| 久久亚洲日韩看片无码| 婷婷久久综合九色综合98| 色青青草原桃花久久综合| 久久亚洲精品视频| 一本久久知道综合久久| 亚洲欧美日韩久久精品| 国产精品久久久久一区二区三区| 久久精品亚洲中文字幕无码麻豆| 一本一道久久a久久精品综合 | 91亚洲国产成人久久精品| 亚洲精品无码专区久久久| 99久久亚洲综合精品成人| 久久91精品国产91久久户| 色婷婷综合久久久中文字幕 | 国产精品久久久久a影院| 国产精品亚洲美女久久久| 好属妞这里只有精品久久| 国内精品久久久久影院一蜜桃| 久久国产欧美日韩精品免费| 久久夜色撩人精品国产| 久久国产综合精品五月天| 久久亚洲2019中文字幕| 18禁黄久久久AAA片| 亚洲人成网亚洲欧洲无码久久 | 国产福利电影一区二区三区久久久久成人精品综合 |