是否已经实现了RAII文件句柄?

tgy*_*tgy 6 c++ io raii

RAII文件句柄看起来非常基本,所以我猜它已经实现了?但我找不到任何实施.我在boost :: iostreams中找到了file_descriptor,但我不知道它是不是我正在寻找的东西.

Ded*_*tor 7

取决于你到底想要什么。

如果您确实想要一个作用域句柄,请使用:

std::unique_ptr<HANDLETYPE, closehandletypefunction> smartpointer;
Run Code Online (Sandbox Code Playgroud)

对于FILE指针,这看起来像

std::unique_ptr<FILE, int (*)(FILE *)> f(fopen("myfile.txt", "a"), fclose);
Run Code Online (Sandbox Code Playgroud)

然后可以FILE*通过 获得f.get()。同样的事情也适用于文件描述符(分别open来自close<fcntl.h><unistd.h>

首选的 C++ 方法是将句柄包装在具有数千个成员的对象中以完成所有操作。


Ton*_*roy 6

std::fstream支持RAII风格的用法 - 它们可以在构造时打开甚至测试,并且它们会在析构函数中自动刷新和关闭,但如果你只是假设它有效,那么你可能会错过错误,所以你可能想要做更明确的事情.代码,如果你需要健壮性.

例如:

if (std::ifstream input(filename))
    ... use input...
else
    std::cerr << "unable to open '" << filename << "'\n";
Run Code Online (Sandbox Code Playgroud)

如果你真的想使用文件描述符,你可以调整类似下面的东西来品尝.它比刚调用的东西长一点close,但如果你想做强大的编程,你需要检查并以某种方式处理错误....

struct Descriptor
{
    Descriptor(int fd, const char* filename = nullptr)
      : fd_(fd), filename_(filename)
    {
        if (fd < 0)
        {
            std::ostringstream oss;
            oss << "failed to open file";
            if (filename_) oss << " '" << filename_ << '\'';
            oss << ": " << strerror(errno);
            throw std::runtime_error(oss.str());
        }
    }
    ~Descriptor()
    {
        if (fd_ != -1 && close(fd_) == -1)
        {
            // throwing from destructors risks termination - avoid...
            std::cerr << "failed to close file";
            if (filename_) std::cerr << " '" << filename_ << '\'';
            std::cerr << ": " << strerror(errno) << std::endl;
        }
    }
    operator int() const { return fd_; }

  private:
    int fd_;
};
Run Code Online (Sandbox Code Playgroud)

用法:

try
{
    Descriptor fd(open(filename, O_RDONLY), filename);
    int nbytes = read(fd, ...);
    ...
}
catch ...
Run Code Online (Sandbox Code Playgroud)