For copying what I read from input file to vector, I used std::copy()
as recommended in Reading an std::ifstream to a vector of lines.
The problem occurs if I use:
std::copy(std::istream_iterator<unsigned char>(inputfile),
std::istream_iterator<unsigned char>(),
std::back_inserter(myVector));
The 16th byte of my file is missing in the myVector
variable.
But if I use the following code:
inputfile.read((char*)&myVector[0], sizeof(int)*getfilesize(nameOfFile));
Then the byte is not missing anymore.
I am trying to parse WAV files and I lost too much time on this, I hope I will learn something new out of this. Can you please tell me what is wrong with the first version of the code above?
sizeof(int)*getfilesize(nameOfFile)
as the second argument ofread
is error-prone. Better use something likemyVector.size() * sizeof(myVector[0])
. – Danelledanete