如何从POSIX文件描述符构造c ++ fstream?

BD *_*ill 88 c++ posix fstream file-descriptor

我基本上是在寻找fdopen()的C++版本.我对此做了一些研究,这似乎应该是容易的事情之一,但事实证明是非常复杂的.我是否遗漏了这种信念(即它真的很容易)?如果没有,是否有一个好的图书馆在那里处理这个?

编辑:将我的示例解决方案移到单独的答案.

Pio*_*ost 69

从ÉricMalenfant给出的答案:

AFAIK,在标准C++中无法做到这一点.根据您的平台,您的标准库实现可能提供(作为非标准扩展)fstream构造函数,将文件描述符作为输入.(这是libstdc ++,IIRC的情况)或FILE*.

基于以上观察和我的研究,下面有两种变体的工作代码; 一个用于libstdc ++,另一个用于Microsoft Visual C++.


的libstdc ++

有非标准的__gnu_cxx::stdio_filebuf类模板,它继承std::basic_streambuf并具有以下构造函数

stdio_filebuf (int __fd, std::ios_base::openmode __mode, size_t __size=static_cast< size_t >(BUFSIZ)) 
Run Code Online (Sandbox Code Playgroud)

with description 此构造函数将文件流缓冲区与打开的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;
}
Run Code Online (Sandbox Code Playgroud)

Microsoft Visual C++

以前有ifstream的构造函数的非标准版本采用POSIX文件描述符,但它缺少当前的文档和代码.还有另一个非标准版本的ifstream构造函数采用FILE*

explicit basic_ifstream(_Filet *_File)
    : _Mybase(&_Filebuffer),
        _Filebuffer(_File)
    {   // construct with specified C stream
    }
Run Code Online (Sandbox Code Playgroud)

它没有记录(我甚至找不到任何旧的文档,它会出现在哪里).我们称之为(第1行),参数是调用_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;
}
Run Code Online (Sandbox Code Playgroud)

  • 由于完整性,现在接受了答案.其他人可能对使用boost的解决方案感兴趣,已将其转移到单独的答案中. (2认同)

Éri*_*ant 40

AFAIK,在标准C++中无法做到这一点.根据您的平台,您的标准库实现可能提供(作为非标准扩展)一个fstream构造函数,它采用文件描述符(这是libstdc ++,IIRC的情况)或FILE*作为输入.

另一种方法是使用boost :: iostreams :: file_descriptor设备,如果你想拥有一个std :: stream接口,你可以将它包装在boost :: iostreams :: stream中.

  • 考虑到这是唯一的便携式解决方案,我不明白为什么这不是被接受或评价最高的答案. (3认同)

Dar*_*ryl 8

您的编译器很有可能提供基于FILE的fstream构造函数,即使它是非标准的.例如:

FILE* f = fdopen(my_fd, "a");
std::fstream fstr(f);
fstr << "Greetings\n";
Run Code Online (Sandbox Code Playgroud)

但据我所知,没有可移植的方法来做到这一点.

  • 请注意,g ++(正确)在c ++ 11模式下不允许这样做 (2认同)

BD *_*ill 8

这个问题的原始(未说明)动机的一部分是能够使用安全创建的临时文件在程序之间或测试程序的两个部分之间传递数据,但是tmpnam()在gcc中引发警告,所以我想要改为使用mkstemp().这是我根据ÉricMalenfant给出的答案编写的测试程序,但是使用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);
  }
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ark 6

这实际上很容易。Nicolai M. Josuttisfdstream与他的书The C++ Standard Library - A Tutorial and Reference 一起发布。您可以在此处找到 184 行实现。

  • 这是“相当简单”一词的有趣应用。 (10认同)