私はベクトルの内容を読み込もうとしているバイナリファイルを持っています。すべてのファイルは同じサイズですが、下のコードを使用すると、最終的なベクトルサイズはファイルサイズよりも少し小さく、ファイルごとに異なります(ファイルごとに同じです)。バイナリファイルをベクトルに読み込むフルファイル未満を読む
Detected foo_binary.bin size: 2113753
Loaded data from foo_binary.bin, with 2099650 elements
2113753数はバイト単位でファイルの正しいサイズである:私はこれを取得、ファイル上でこれをしようと...ここで何が起こっているかで
#include <fstream>
#include <vector>
#include <iostream>
#include <iterator>
int main(int argc, char *argv[]) {
std::string filename(argv[1]);
// Get file size
std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
int size = (int)ifs.tellg();
std::cout << "Detected " << filename << " size: " << size << std::endl; // seems correct!
// Load file
ifs.seekg(0, std::ios::beg);
std::istream_iterator<char unsigned> start(ifs), end;
std::vector<char unsigned> v;
v.reserve(size);
v.assign(start, end);
std::cout << "Loaded data from " << filename << ", with " << v.size() << " elements" << std::endl;
}
混乱しています。
同じサイズの別のファイルでこれを試してみると、ベクトルサイズは2100700個の要素になります。もう少しですが、ファイル全体ではありません。
ここでは何が起こっていますか?
[thses](http://stackoverflow.com/questions/7241871/loading-a-file-into-a-vectorchar)メソッドのいずれかを使用して同じことを取得しますか? – NathanOliver