2011-11-02 117 views
2

我不知道逐行读取大型文本文件(例如,std :: getline或fgets)是否可以使用预定义读取缓冲区大小进行缓冲,或者必须使用特殊的按字节函数?C++缓冲文件读取

我的意思是读取非常大的文件与I/O操作数量优化(例如,一次从HDD读取32 MB)。当然,我可以手动进行缓冲读取,但我认为标准文件流有这种可能性。

+0

编写自定义函数。这就是创新如何:) – Kris

回答

4

无论是逐行还是特殊的逐字节功能。相反,下面应该做你的工作:

std::ifstream file("input.txt"); 
std::istream_iterator<char> begin(file), end; 

std::vector<char> buffer(begin, end); //reading the file is done here! 
//use buffer. it contains the content of the file! 

大功告成,为buffer包含该文件的内容。