从std::fstream获得文件描述符

核心:

  auto helper = [](std::filebuf& fb) -> int {
    class Helper : public std::filebuf {
    public:
      int handle() { return _M_file.fd(); }
    };

    return static_cast(fb).handle();
  };

示例:

#include 
#include 
#include 
#include 

int main()
{
  std::string str("Hello World");

  std::ofstream fs("path",
    std::ofstream::binary | std::ofstream::out | std::ofstream::in);

  if (!fs.is_open())
    fs.open("path",
      std::ofstream::binary | std::ofstream::out);

  auto helper = [](std::filebuf& fb) -> int {
    class Helper : public std::filebuf {
    public:
      int handle() { return _M_file.fd(); }
    };

    return static_cast(fb).handle();
  };

  int fd = helper(*fs.rdbuf());

  fs.seekp(0, fs.beg);
  fs.write(str.data(), str.length());
  fsync(fd);
  fs.close();

  return 0;
}

你可能感兴趣的:(从std::fstream获得文件描述符)