我基本上正在寻找fdopen()的C++版本。我对此进行了一些研究,它似乎是应该很容易的事情,但结果却非常复杂。我对这种想法是否有所遗漏(即它确实很容易)?如果没有,那么有没有一个很好的库可以处理这个问题?
编辑:将我的示例解决方案移动到单独的答案中。
我基本上正在寻找fdopen()的C++版本。我对此进行了一些研究,它似乎是应该很容易的事情,但结果却非常复杂。我对这种想法是否有所遗漏(即它确实很容易)?如果没有,那么有没有一个很好的库可以处理这个问题?
编辑:将我的示例解决方案移动到单独的答案中。
根据Éric Malenfant给出的答案:
据我所知,在标准C++中没有办法实现这一点。根据您的平台,您的标准库实现可能会提供(作为非标准扩展)以文件描述符作为输入的fstream构造函数。(这是libstdc++的情况,如果我没记错的话)或者FILE*。
基于上述观察和我的研究,有两个变体的可用代码;一个是针对libstdc++,另一个是针对Microsoft Visual C++。
有一个非标准的__gnu_cxx::stdio_filebuf
类模板,它继承了std::basic_streambuf
并具有以下构造函数。
stdio_filebuf (int __fd, std::ios_base::openmode __mode, size_t __size=static_cast< size_t >(BUFSIZ))
带有描述此构造函数将文件流缓冲区与打开的POSIX文件描述符相关联。
我们通过传递POSIX句柄(第1行)来创建它,然后将其作为basic_streambuf(第2行)传递给istream的构造函数:
#include <ext/stdio_filebuf.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream ofs("test.txt");
ofs << "Writing to a basic_ofstream object..." << endl;
ofs.close();
int posix_handle = fileno(::fopen("test.txt", "r"));
__gnu_cxx::stdio_filebuf<char> filebuf(posix_handle, std::ios::in); // 1
istream is(&filebuf); // 2
string line;
getline(is, line);
cout << "line: " << line << std::endl;
return 0;
}
以前有一个非标准的版本,它使用POSIX文件描述符来构造ifstream,但它既不在当前文档中,也不在代码中。还有另一个非标准的ifstream构造函数版本,它使用FILE*作为参数。
explicit basic_ifstream(_Filet *_File)
: _Mybase(&_Filebuffer),
_Filebuffer(_File)
{ // construct with specified C stream
}
这个内容并没有记录(我甚至找不到任何旧的文档中有它的存在)。我们使用参数调用它(第一行),该参数是通过调用_fdopen来从POSIX文件句柄获取C流FILE*的结果。
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream ofs("test.txt");
ofs << "Writing to a basic_ofstream object..." << endl;
ofs.close();
int posix_handle = ::_fileno(::fopen("test.txt", "r"));
ifstream ifs(::_fdopen(posix_handle, "r")); // 1
string line;
getline(ifs, line);
ifs.close();
cout << "line: " << line << endl;
return 0;
}
std::cout
的实现是个好主意。我在想 stdio_filebuf
和 stdio_sync_filebuf
之间有什么区别? - Piotr Dobrogost_sync_...
版本直接将调用翻译为底层的FILE*
,不像普通的iostreams会进行一些缓冲。我想补充的是,这段代码泄漏了FILE
s,通常情况下你不会以这种方式使用它(或者,你可以使用FILE*
来初始化缓冲区),但整个过程对于让iostream
与socket()
、pipe()
、memfd_create()
之类的fd进行通信非常有用。 - jpalecek_sync_...
版本直接将调用翻译为底层的FILE*
,而不像普通的iostreams那样进行一些缓冲。我想补充一下,这段代码会泄漏FILE
,通常情况下你不会这样使用(或者,你可以使用FILE*
来初始化缓冲区),但整个过程对于让iostream
与socket()
、pipe()
、memfd_create()
创建的fd进行通信非常有用。 - undefinedFILE*
作为输入的构造函数。boost::iostreams
似乎不支持移动操作。因此,在现代C++中,我认为它的可用性显著降低了。 - Louis Langholtz这个问题最初的(未声明的)动机之一是能够通过安全创建的临时文件在程序之间或测试程序的两个部分之间传递数据,但tmpnam()在gcc中会抛出警告,因此我想使用mkstemp()代替。以下是我根据Éric Malenfant的答案编写的测试程序,但使用mkstemp()而不是fdopen();这在我安装了Boost库的Ubuntu系统上运行正常:
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
using boost::iostreams::stream;
using boost::iostreams::file_descriptor_sink;
using boost::filesystem::path;
using boost::filesystem::exists;
using boost::filesystem::status;
using boost::filesystem::remove;
int main(int argc, const char *argv[]) {
char tmpTemplate[13];
strncpy(tmpTemplate, "/tmp/XXXXXX", 13);
stream<file_descriptor_sink> tmp(mkstemp(tmpTemplate));
assert(tmp.is_open());
tmp << "Hello mkstemp!" << std::endl;
tmp.close();
path tmpPath(tmpTemplate);
if (exists(status(tmpPath))) {
std::cout << "Output is in " << tmpPath.file_string() << std::endl;
std::string cmd("cat ");
cmd += tmpPath.file_string();
system(cmd.c_str());
std::cout << "Removing " << tmpPath.file_string() << std::endl;
remove(tmpPath);
}
}
很有可能你的编译器提供了基于FILE的fstream构造函数,尽管它不是标准的。例如:
FILE* f = fdopen(my_fd, "a");
std::fstream fstr(f);
fstr << "Greetings\n";
其实这很容易。Nicolai M. Josuttis在他的书《C++标准库-教程与参考》中发布了fdstream
。你可以在这里找到184行的实现。
我尝试了Piotr Dobrogost提出的关于libstdc++的解决方案,发现它有一个痛苦的缺陷:由于istream没有适当的移动构造函数,因此很难将新构建的istream对象从创建函数中取出。另一个问题是它会泄漏FILE对象(即使不是底层posix文件描述符)。这里有一个替代方案,可以避免这些问题:
#include <fstream>
#include <string>
#include <ext/stdio_filebuf.h>
#include <type_traits>
bool OpenFileForSequentialInput(ifstream& ifs, const string& fname)
{
ifs.open(fname.c_str(), ios::in);
if (! ifs.is_open()) {
return false;
}
using FilebufType = __gnu_cxx::stdio_filebuf<std::ifstream::char_type>;
static_assert( std::is_base_of<ifstream::__filebuf_type, FilebufType>::value &&
(sizeof(FilebufType) == sizeof(ifstream::__filebuf_type)),
"The filebuf type appears to have extra data members, the cast might be unsafe");
const int fd = static_cast<FilebufType*>(ifs.rdbuf())->fd();
assert(fd >= 0);
if (0 != posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL)) {
ifs.close();
return false;
}
return true;
}
调用posix_fadvise()函数展示了它的潜在用途。另外请注意,该示例使用了C++11中的static_assert和using,除此之外,它应该可以在C++03模式下正常构建。
dynamic_cast
呢? - jpalecekdynamic_cast
呢? - undefined另一种不可移植的解决方案是使用mmap
(或其Windows模拟),然后从mmap
给出的指针构造std::iostream
(像这样)。
是的,它并没有完全构造一个std::fstream
,但是这个要求很少需要满足,因为每个代码片段都应该依赖于流接口(例如std::istream
),而不是它们的实现。
我认为这种解决方案比使用STL实现特定的黑客技巧更具可移植性,因为这样你只依赖于操作系统,而不是同一操作系统上特定的STL实现。
我的理解是,在C++ iostream对象模型中,没有与FILE指针或文件描述符相关联的关系,以保持代码的可移植性。
话虽如此,我看到有几个地方提到使用mds-utils或boost来帮助弥合这一差距。
mmap
将文件映射到内存中,并将其内容作为字节数组公开。 - daparic