这是我到目前为止所尝试的,向量根本没有被填充:
#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;
}
但是,在子句read()
内部使用以下代码可以正常工作:else
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, " " )
);
std::copy()
如第一个代码块所示,我是否缺少一些东西来填充向量?