gpt4 book ai didi

c# - Filestream 寻找正确的位置而 fstream 没有

转载 作者:行者123 更新时间:2023-11-28 06:16:20 24 4
gpt4 key购买 nike

在 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 并读取任何内容?

参见 reference on mapped_file_source

Overview: By default, files must exist before being opened, and are not truncated; attempting to write past the end of a file results in an error.

Constructor documentation:length - The number of bytes to map. If this parameter is not specified, the entire file is mapped.

关于c# - Filestream 寻找正确的位置而 fstream 没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30228872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com