无法打开具有boost::iostreams::file的文件
Cannot open file with boost::iostreams::file
我想用boost::iostreams::file打开一个文件,代码为:
boost::iostreams::file file("test.txt");
if(!file.is_open()) {
throw std::runtime_error("Could not open file");
}
但它不会打开文件,我不知道为什么。当我使用boost::iostreams::file_sink时,它是有效的。也许你知道怎么了?我忘了什么吗?我使用的是Boost版本1.60
查看iostreams/device/file.hpp,我们可以看到构造函数提供了in|out
的默认打开模式。
basic_file( const std::string& path,
BOOST_IOS::openmode mode =
BOOST_IOS::in | BOOST_IOS::out,
BOOST_IOS::openmode base_mode =
BOOST_IOS::in | BOOST_IOS::out );
并且它以这种模式调用CCD_ 2方法。
template<typename Ch>
basic_file<Ch>::basic_file
( const std::string& path, BOOST_IOS::openmode mode,
BOOST_IOS::openmode base_mode )
{
open(path, mode, base_mode);
}
然后,open(...)
方法使用此模式创建impl
的新实例。
template<typename Ch>
void basic_file<Ch>::open
( const std::string& path, BOOST_IOS::openmode mode,
BOOST_IOS::openmode base_mode )
{
pimpl_.reset(new impl(path, mode | base_mode));
}
该实现使用std::basic_filebuf
作为文件I/O。
struct impl {
impl(const std::string& path, BOOST_IOS::openmode mode)
{ file_.open(path.c_str(), mode); }
~impl() { if (file_.is_open()) file_.close(); }
BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) file_;
};
在iostreams/detail/fstream.hpp中定义的宏:
# define BOOST_IOSTREAMS_BASIC_FILEBUF(Ch) std::basic_filebuf<Ch>
现在,根据std::basic_filebuf
的文档(或者具体地说,它的open(...)
方法):
openmode & ~ate Action if file already exists Action if file does not exist ------------------------------------------------------------------------------ out|in Read from start Error
为了在不存在新文件的情况下创建新文件,您需要提供适当的打开模式。在您的情况下,这将意味着in|out|app
或in|out|trunc
,具体取决于您希望对现有文件执行的操作。
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 隐式声明的 boost::iostreams::mapped_file_source 已被弃用
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- Visual Studio C++ Project File CustomBuild Task: Filter Outp
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 可视化 如何在C++中将字符数组转换为 FILE 类型
- 如何在从文件中读取整数时使用 file.eof()?
- "No such file or directory" C++标头问题
- 使用 sftp_open() 使用 C++ 中的 SFTP libssh 将文件从本地复制到远程时,File 返回 N
- Visual Studio "fatal error LNK1104: cannot open file"未使用或包含的库
- typedef vector size_type in header file
- make 命令创建 .file,但不创建应用程序文件
- 为什么 VScode 在 .h 文件中显示"'iostream' file not found"?
- 我在 #include < bits/stdc++h.> 放置时出错"No such file or directory"
- 使用 编译时。RES File with MinGW G++, .无法识别 RES
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- 叮叮当当。target_include_directories "No such file or directory"
- 无法打开具有boost::iostreams::file的文件