I have binary files whose contents I'm trying to read into a vector. All the files are the same size, yet using my code below, the final vector size is always a little bit smaller than the file size, and it's different from file to file (but same for each file). I'm confused at what is going on here...
#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;
}
Trying this on a file, I get this:
Detected foo_binary.bin size: 2113753
Loaded data from foo_binary.bin, with 2099650 elements
The 2113753 number is the correct size of the file in bytes.
Trying this on another file of the same size, the vector size will end up having 2100700 elements. A bit more, but again not the whole file.
What's going on here?
Reading Binary FileUse the 'rb' mode in the open() function to read a binary files, as shown below.
Reading a Binary File Into LiveCode The same holds true when writing a file to disk as well. You can choose one of two methods for loading the data. 1) Use the commands open file, read from file and close file. 2) Use the URL keyword with the put command, prefixing the file path with "binfile:".
There are multiple stream iterators. The class template std::istream_iterator<T>
is for formatted input, i.e., it is going go skip leading whitespace before trying to read an object of type T
.
From the looks of it you want std::istreambuf_iterator<char>
which is used to iterate over the characters in a file, not doing any skipping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With