2011-12-05 28 views

回答

2

如果文件很大,爲什麼要讀一遍,將文件整個存入內存?

您可以每次閱讀一小段文字。尺寸與在此所述FUNC尺寸確定

file.read(buff, size) 

拋光輪是char的數組。

對不起,但你不能最簡單的讀/保存矢量到文件。 瞭解更多詳情see herehere

而使用谷歌,這是非常有幫助...

-1

你沒給的你想在你的問題做過多的上下文。但這裏有一個快速的&骯髒的方式做到這一點:

#include <iterator> 
#include <fstream> 
#include <vector> 
#include <assert.h> 
using namespace std; 

const char *filename = "foo.bar"; 
int main() 
{ 
    vector<bool> v; 
    ifstream binary_file(filename, ios::binary); 

    assert(binary_file); 
    copy(istream_iterator<unsigned char>(binary_file), 
     istream_iterator<unsigned char>(), 
     back_insert_iterator< vector<bool> >(v)); 
} 

讀零字節「\ 0」字符到載體將是錯誤的。讀入的任何其他字節都將被視爲true。