为了将我从输入文件读取的内容复制到 vector ,我使用了 std::copy()
,如 Reading an std::ifstream to a vector of lines 中所推荐的那样.
如果我使用会出现问题:
std::copy(std::istream_iterator<unsigned char>(inputfile),
std::istream_iterator<unsigned char>(),
std::back_inserter(myVector));
我的文件的第 16 个字节在 myVector
变量中丢失。
但是如果我使用下面的代码:
inputfile.read((char*)&myVector[0], sizeof(int)*getfilesize(nameOfFile));
那么这个字节就不会再丢失了。
我正在尝试解析 WAV 文件,但我在这上面浪费了太多时间,我希望我能从中学到一些新东西。你能告诉我上面代码的第一个版本有什么问题吗?
最佳答案
istream_iterator
使用 operator >>
读取元素,但 operator >>
跳过空格。
您可以尝试使用 noskipws
inputfile >> noskipws;
§ 24.6.1 p1。 (我的重点)
The class template
istream_iterator
is an input iterator (24.2.3) that reads (using operator>>) successive elements from the input stream for which it was constructed....
关于c++ - 将 std::ifstream 读取到行 vector 后,某些字节丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776023/