I've come across an odd problem that I haven't seen before or haven't notice before. I'm trying to use an ifstream to read binary data into a vector. My test code is shown below:
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::ifstream ifs("foo.bin", std::ios::binary);
ifs.seekg(0, std::ios::end);
uint32_t size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<uint8_t> data;
if (size == 0)
{
printf("Size can't be zero\n");
}
else
{
data.reserve(size);
data.insert(data.begin(),
std::istream_iterator<uint8_t>(ifs),
std::istream_iterator<uint8_t>());
}
for (int i = 0 ; i < data.size(); i++)
{
std::cout << std::hex << (int)data[i] << " ";
}
std::cout << std::endl;
}
I then feed in a binary blob with 20 bytes. Hexdump output below:
00000000 55 66 77 0a 99 aa 0d 30 31 33 34 00 00 11 22 33 |Ufw....0134..."3|
00000010 44 55 66 77 |DUfw|
00000014
I compile this program and run it and get the following 18 bytes
./main
55 66 77 99 aa 30 31 33 34 0 0 11 22 33 44 55 66 77
Notice how the 0x0A and 0x0D have been removed from the vector?