C++快速讀取大文件

#include <ctime>
#include <ios>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

/**
 * 快速讀取大文件
 * @return
 */
int main()
{
    clock_t start = clock();
    ifstream fin("d:\\1.txt", std::ios::binary);

    vector<char> buf(static_cast<unsigned int>(fin.seekg(0, std::ios::end).tellg()));
    fin.seekg(0, std::ios::beg).read(&buf[0], static_cast<std::streamsize>(buf.size()));

    fin.close();
    clock_t end = clock();
    cout << "time : " << ((double) end - start) / CLOCKS_PER_SEC << "s\n";
    vector<char>::iterator it;
    for (it = buf.begin(); it != buf.end(); it++)
    {
        cout << *it << "";
    }
    cout << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章