文件流寻求正确的位置,而 fstream 不会
Filestream seeks correct position whereas fstream doesn't
在C#
中FileStream fs("file.bin",/*Open in binary, read only mode*/);
var bytes = new byte[100];
fs.Seek(20000000, SeekOrigin.Begin); //*20000000*
fs.Read(bytes, 0, 100);
在C 中,istream
总是变为null
typedef std::shared_ptr<boost::iostreams::mapped_file_source> FileStream;
FileStream fs = FileStream(new boost::iostreams::mapped_file_source("file.bin", 100, 0));
if (fs->is_open())
{
boost::iostreams::stream<boost::iostreams::mapped_file_source> is(*fs.get());
if (is.seekg(20000000, is.beg))
//read 100 characters from 20000000th position
fs->close();
}
如果我更改 boost::iostreams::stream<boost::iostreams::mapped_file_source> is(*fs.get());
进入 boost::iostreams::stream<boost::iostreams::mapped_file_source> is("file.bin");
is
是初始化的,但读取到20000000th
字节仍然是不可能的。错误发生与未找到文件时完全相同。
您被淘汰告诉mapped_file_source
,要映射的最大文件大小为100字节。那么,为什么您认为您可以进入20000000的位置并阅读任何内容?
请参阅mapped_file_source上的参考
概述:默认情况下,打开之前必须存在文件,并且不被截断;试图通过文件的末尾写入错误会导致错误。
构造函数文档:长度 - 要映射的字节数。如果未指定此参数,则将映射整个文件。
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 在fstream期间忽略空行
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- 使用 fstream 将文件数据从当前位置保存到文件末尾
- 文件流寻求正确的位置,而 fstream 不会
- fstream搜索:位置保持在-1
- 访问冲突读取位置fstream read
- 在二进制整数 (C++) 中读取后,fstream 是否移动到下一个位置