我使用mapped_file_source
从boost::iostreams
namespace
从块大文件读取:
boost::iostreams::mapped_file_source read_bytes(const char *file_path,
unsigned long long int offset,
unsigned long long int length) {
iostreams::mapped_file_params parameters;
parameters.path = file_path;
parameters.length = static_cast<size_t>(length);
parameters.flags = iostreams::mapped_file::mapmode::readonly;
parameters.offset = static_cast<boost::iostreams::stream_offset>(offset);
boost::iostreams::mapped_file_source file;
file.open(parameters);
if (file.is_open()) {
return file;
} else {
printf("Failed to open file\n");
exit(EXIT_FAILURE);
}
}
我的代码工作正常,在Ubuntu
WSL
(Windows Subsystem for Linux
),但是当我编译和运行在Windows上它,第二file.open
调用导致程序退出码3
退出。
Reading file in 5 parts
Processing chunk 1/5
Processing chunk 2/5
Process finished with exit code 3
没有错误消息或异常已经引起。该documentation表明,它是ERROR_PATH_NOT_FOUND
但没有任何意义。
我调试两个平台的二进制文件和所有的变量都具有唯一的例外是在Unix
风格的文件路径和Windows
风格的路径以及分配的地址和系统时间变量,所以没有内存损坏发生完全相同。我不明白为什么这不会对Windows
工作时,它应该表现相同。
我使用MinGW
编译为Windows
gcc 8.2
和Ubuntu
。
"C:\Program Files\mingw-w64\x86_64-8.1.0-win32-seh-rt_v6-rev0\mingw64\bin\x86_64-w64-mingw32-gcc.exe" --version
x86_64-w64-mingw32-gcc.exe (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
如果我在一个单一的去读取这个文件,它工作正常(!)。我对齐所有偏移到页面大小的倍数。当它超出范围,所以它不是一个“文件已打开”的问题(这将导致实际由mapped_file_source
除外)的Boost
自动关闭。
使用MSVC
问题不能再被复制现在。在一般情况下,使用Microsoft
的编译器Windows
可能比MinGW
的喜欢更可靠,尤其是因为我是用an "unofficial" toolchain。