如何检测 ofstream 是否正在写入 /dev/null

How to detect if ofstream is writing to /dev/null

是否有任何(简单的)方法可以在 some_function() 中检测 ofs 是否正在写入 /dev/null

#include <fstream>

some_function(std::ofstream & ofs);

int main()
{
    std::ofstream ofs("/dev/null");
    ofs << "lorem ipsum";

    some_function(ofs); // Testing in here

    return 0;
}

is there any (simple) way to detect in some_function(std::ofstream ofs) if ofs is writing to /dev/null or not?

没有,没有。

您正在寻找获取该信息的方法这一事实向我表明 some_function 有分支代码,具体取决于您是否正在写入 /dev/null

您可以通过向函数添加另一个参数并让客户端代码向您提供该信息来解决该问题。

void some_function(std::ofstream& ofs, bool isDevNull);

并将其用作:

std::ofstream ofs ("/dev/null", std::ofstream::out);
ofs << "lorem ipsum";
some_function(ofs, true);

来自std::ofstream,没有

来自 FILE*,是的,但它不可移植。

这是 linux 的版本:

#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <memory>
#include <stdexcept>
#include <iostream>
#include <sstream>

struct file_closer
{
    void operator()(FILE*p) const noexcept
    {
        if (p)
            fclose(p);
    }
};

auto open_write(const char* path) -> std::unique_ptr<FILE, file_closer>
{
    auto result = std::unique_ptr<FILE, file_closer>(fopen(path, "w"));

    if (!result.get())
        throw std::runtime_error("not opened");
    return result;
}

size_t do_readlink(const char* path, char* buffer, size_t buflen)
{
    auto len = readlink(path, buffer, buflen);
    if (len < 0)
        throw std::runtime_error("failed to read link");
    return size_t(len);
}

bool is_dev_null(FILE* fp)
{
    int fd = fileno(fp);
    std::ostringstream procpath;
    procpath << "/proc/self/fd/" << fd;
    auto spath = procpath.str(); 

    size_t bufs = 1024;
    std::string path(bufs, ' ');
    auto len = do_readlink(spath.c_str(), &path[0], bufs);
    while(len > bufs)
    {
        bufs = len;
        path.resize(bufs);
        len = do_readlink(spath.c_str(), &path[0], bufs);
    }

    path.resize(len);
    return path == "/dev/null";
}

int main()
{
    auto fp = open_write("/dev/null");
    std::cout << is_dev_null(fp.get());
}