現代的なファイルサイズは大きすぎて、intやlongでは表現できないかもしれません。
現在のC++のI/Oストリームはlong long型が組み込まれる前に導入されたのでこういう問題があります。
この問題が、N2926の提案で解決されます。
変更点としてはchar_traitsの
typedef OFF_T off_type; typedef POS_T pos_type;
となっていたものを以下のように修正し
typedef unspecified-type off_type; typedef unspecified-type pos_type;
<ios>にある以下のtypedefを
typedef OFF_T streamoff; typedef POS_T streamsize;
以下のように修正されるくらいです。
typedef implementation-defined streamoff; typedef implementation-defined streamsize;
Microsoft Visual C++ 2010 beta 1ではすでにこの提案を実装していて
以下のプログラムはエラーなく動作するそうです。
#include <fstream> #include <iostream> #include <iosfwd> const long long max = 800000000LL; int main() { std::fstream file("test.file", std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc); if (!file) std::cout << "Could not open test.file\n"; for (long long i = 0; i < max; ++i) file.write(reinterpret_cast<char*>(&i), sizeof(i)); long long x; file.seekg((max - 1) * sizeof(x), std::ios_base::beg); file.read(reinterpret_cast<char*>(&x), sizeof(x)); if (x != (max - 1)) std::cout << "seekg with offset failed to position the file correctly\n"; std::fstream::pos_type pos((max - 2) * sizeof(x)); file.seekg(pos); file.read(reinterpret_cast<char*>(&x), sizeof(x)); if (x != (max - 2)) std::cout << "seekg with pos_type failed to position the file correctly\n"; return 0; }
N2926 C++0x Stream Positioning - Revision 1
library issue 573. C++0x file positioning should handle modern file sizes