ファイル全体読み込み
自分が書くとこういう感じですかね。
#include <iostream> #include <fstream> #include <iterator> #include <iomanip> #include <vector> int main() { std::ifstream ifs("file_read.cpp"); if (!ifs) { std::cerr << "file can not open." << std::endl; return -1; } std::istreambuf_iterator<char> head(ifs), tail; std::vector<char> buffer(head, tail); std::cout << "file size: " << buffer.size() << " byte(s)." << std::endl; std::cout.fill('0'); std::cout << std::hex; for (size_t position = 0; position < buffer.size(); ++position) { if ((position % 16) == 0) std::cout << std::endl; std::cout << std::setw(2) << static_cast<int>(buffer[position]) << ", "; } std::cout << std::endl; return 0; }
普段はあまりもの考えたくないので上記のコードのように読んでます。
istreambuf_iteratorでバッファリングされてるのでスピードもそれなりです。
でもやっぱり低レベル層にちかい方法だと思われるseekg()してread()のほうが早いのでパフォーマンス必要なところだとseekg()/read()のほうがいいでしょうけど。