• <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++博客 首頁 新隨筆 聯系 聚合 管理
              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 閱讀(2123) 評論(6)  編輯 收藏 引用

            Feedback

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

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

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

            // 和你的功能一樣,把文件內容讀到一個容器中
            {
            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) );
            }

            // 把文件內容讀到一個新文件中
            {
            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) );
            }
            }
              回復  更多評論
              

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

            認真讀了星兄的回復,覺得受益非淺。

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

            "直接把文件內容讀到data中" is a good idea .

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

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

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

            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;
            }
              回復  更多評論
              

            # 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));  回復  更多評論
              

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

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

              回復  更多評論
              

            精品久久久久久久久久久久久久久| 久久久中文字幕日本| 三级片免费观看久久| 久久久久一级精品亚洲国产成人综合AV区 | 久久99国产综合精品女同| 久久综合给合久久狠狠狠97色| 国产精品久久久亚洲| 国产精自产拍久久久久久蜜| 久久99热这里只频精品6| 久久综合亚洲欧美成人| 久久久久无码精品国产app| 无码人妻久久久一区二区三区| 久久电影网2021| 香蕉久久夜色精品升级完成| 91性高湖久久久久| 久久精品国产男包| 久久青青草原精品国产软件| 久久精品亚洲中文字幕无码麻豆| 狠狠人妻久久久久久综合| 天堂久久天堂AV色综合| 久久青青草原精品国产软件| 久久久噜噜噜www成人网| 久久久艹| 久久精品成人免费国产片小草| 狠狠色婷婷久久一区二区三区| 婷婷国产天堂久久综合五月| 国产日韩欧美久久| 国产精品青草久久久久婷婷| 久久久久av无码免费网| 午夜视频久久久久一区| 久久精品18| 婷婷国产天堂久久综合五月| 无码乱码观看精品久久| 久久久国产精华液| 久久人人爽人人澡人人高潮AV| 精品久久国产一区二区三区香蕉| 国产精品久久久久9999高清| 久久精品国产亚洲77777| 韩国免费A级毛片久久| 俺来也俺去啦久久综合网| 国产日产久久高清欧美一区|