您如何从文件中读取n个字节并将其放入vector <uint8_t>中?使用迭代器? [英] How do you read n bytes from a file and put them into a vector<uint8_t> using iterators?
本文介绍了您如何从文件中读取n个字节并将其放入vector <uint8_t>中?使用迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基于此问题:
答案是:
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::basic_ifstream<BYTE> file(filename, std::ios::binary);
// read the data:
return std::vector<BYTE>((std::istreambuf_iterator<BYTE>(file)),
std::istreambuf_iterator<BYTE>());
}
将整个文件读入向量.
我要执行的操作是一次(例如)在向量中读取100个字节,然后执行填充操作,然后将接下来的100个字节读取到向量中(清除之间的向量).我看不到如何指定要读取的文件数量(即如何设置迭代器).那有可能吗?
What I want to do is read (for example) 100 bytes at a time in the vector, then do stuff, and then read the next 100 bytes into the vector (clear the vector between). I don't see how to specify how much of the file to read (i.e. how to setup the iterators). Is that even possible?
我试图避免编写自己的代码循环来一次复制每个字节.
I am trying to avoid having to write my own code loop to copy each byte at a time.
推荐答案
您可以为此使用 ifstream :: read
.
std::vector<BYTE> v(100);
while ( file.read(reinterpret_cast<char*>(v.data()), 100) )
{
// Find out how many characters were actually read.
auto count = file.gcount();
// Use v up to count BTYEs.
}
这篇关于您如何从文件中读取n个字节并将其放入vector <uint8_t>中?使用迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文