Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 filestream到矢量,文件大小错误_C++11_Vector_Filestream_Filesize - Fatal编程技术网

C++11 filestream到矢量,文件大小错误

C++11 filestream到矢量,文件大小错误,c++11,vector,filestream,filesize,C++11,Vector,Filestream,Filesize,我找到了许多关于这个问题的文章,但没有一篇文章对此进行了足够详细的解释,我对streams还没有经验: 我想将一个文件流式传输到一个向量,这个向量已经被定义,并且包含一些数据 这段代码似乎起作用(它不起作用): std::ifstream fileInputStream(path.wc_str(),std::ios::binary); //字节向量包含一些数据,类型为:std::vector* 字节向量->插入(字节向量->结束(), std::istream_迭代器(fileInputStre

我找到了许多关于这个问题的文章,但没有一篇文章对此进行了足够详细的解释,我对streams还没有经验: 我想将一个文件流式传输到一个向量,这个向量已经被定义,并且包含一些数据

这段代码似乎起作用(它不起作用):

std::ifstream fileInputStream(path.wc_str(),std::ios::binary);
//字节向量包含一些数据,类型为:std::vector*
字节向量->插入(字节向量->结束(),
std::istream_迭代器(fileInputStream),
std::istreamu迭代器();
在本文中,我找到了一种获取文件长度的方法:

std::ifstream fileInputStream;
fileInputStream.open(path.wc_str(),std::ios::in | std::ios::binary);
忽略(std::numeric_limits::max());
std::streamsize fileLength=fileInputStream.gcount();
fileInputStream.clear();//因为忽略将设置eof。
seekg(0,std::ios\u base::beg);
如果我比较第一个代码段的vector->size和第二个代码段的fileLength,我的vector大约短2KB

我希望避免将数据从一个缓冲区复制到另一个缓冲区,因此如果我需要更多的缓冲区来读取所有数据,我更喜欢std::move或类似的东西。有人知道我的第一个片段出了什么问题,或者如何用另一种方式来完成


我应该将文件读入另一个向量缓冲区并将该向量移到第一个向量缓冲区的末尾吗?

std::istream\u迭代器是一个跳过空白的格式化输入迭代器,这就是向量短的原因

相反,它会逐字读取数据


请注意,模板参数的含义在这两个迭代器之间非常不同。在后一种情况下,它是由解码的符号类型(例如,您可能希望将utf-8编码文件解码为
wchar\u t
序列)<代码>标准::istreambuf_迭代器执行身份解码。C++流已经使用了char < /C> >类型来表示二进制数据(例如,参见),

实际上是有效的!你能给我多一点信息吗,为什么用它来代替?我插入数据的向量应该需要@Natulux为您添加注释。
std::ifstream fileInputStream(path.wc_str(), std::ios::binary);
//byteVector contains some data and is of type: std::vector<unsigned char>*
byteVector->insert(byteVector->end(), 
  std::istream_iterator<unsigned char>(fileInputStream), 
  std::istream_iterator<unsigned char>());
std::ifstream fileInputStream;
fileInputStream.open(path.wc_str(), std::ios::in | std::ios::binary);
fileInputStream.ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize fileLength = fileInputStream.gcount();
fileInputStream.clear();   //  Since ignore will have set eof.
fileInputStream.seekg(0, std::ios_base::beg);