This is what I've tried so far, the vector is not getting populated at all:
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream file("E:\\test3.wav", std::ios::binary );
if( file.fail() )
{
std::cout << "File does not exist or could not open file";
}
else
{
std::vector<short> buffer;
//read
std::copy(
std::istream_iterator<short>( file ),
std::istream_iterator<short>(),
std::back_inserter( buffer )
);
//size outputs to 0
std::cout << buffer.size();
}
return 0;
}
However the following code works just fine using read()
inside the else
clause:
std::vector<short> buffer( 56 );
//read
file.read( (char *) &buffer[0], 56 );
//outputs the whole file with all expected values showing.
std::copy(
buffer.begin(),
buffer.end(),
std::ostream_iterator< short >( std::cout, " " )
);
Is there something I'm missing to get std::copy()
to populate the vector as shown in the first block of code?