是否有(跨平台)方法从C ++ std :: fstream获取C FILE *句柄?

我问的原因是因为我的C ++库接受fstreams,而在一个特定的函数中,我想使用一个接受FILE *的C库。

有帮助吗?

解决方案

简短的回答是否定的。

原因是因为std::fstream不需要使用FILE*作为其实现的一部分。因此,即使您设法从funopen()对象中提取文件描述符并手动构建FILE对象,那么您将遇到其他问题,因为您现在将有两个缓冲对象写入同一文件描述符。

真正的问题是为什么要将std::stream对象转换为FILE

虽然我不推荐它,但您可以尝试查找<=>。
不幸的是,这不是不是 POSIX API(它是BSD扩展),因此它的可移植性存在问题。这也是为什么我找不到任何用这样的对象包裹<=>的人。

FILE *funopen(
              const void *cookie,
              int    (*readfn )(void *, char *, int),
              int    (*writefn)(void *, const char *, int),
              fpos_t (*seekfn) (void *, fpos_t, int),
              int    (*closefn)(void *)
             );

这允许您构建<=>对象并指定一些将用于执行实际工作的函数。如果编写适当的函数,可以让它们从实际打开文件的<=>对象中读取。

其他提示

没有标准化的方式。我假设这是因为C ++标准化组不想假设文件句柄可以表示为fd。

大多数平台确实提供了一些非标准的方法来实现这一目标。

http://www.ginac.de/~kreckel/fileno/ 提供对情况的良好描述,并提供隐藏所有平台特定粗糙度的代码,至少对于GCC而言。鉴于这对GCC来说有多严重,我想如果可能的话,我会避免这样做。

  

更新:请参阅@Jettatura我认为这是最佳答案 https://stackoverflow.com/a/33612982/225186 (仅限Linux?)。

ORIGINAL:

(可能不是跨平台,但很简单)

简化 http://www.ginac.de/~kreckel/fileno/中的黑客攻击(dvorak回答),并查看此gcc扩展 http://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a00069.html#a59f78806603c619eafcd4537c920f859 , 我有这个解决方案适用于GCC(至少4.8)和clang(至少3.3)

#include<fstream>
#include<ext/stdio_filebuf.h>

typedef std::basic_ofstream<char>::__filebuf_type buffer_t;
typedef __gnu_cxx::stdio_filebuf<char>            io_buffer_t; 
FILE* cfile_impl(buffer_t* const fb){
    return (static_cast<io_buffer_t* const>(fb))->file(); //type std::__c_file
}

FILE* cfile(std::ofstream const& ofs){return cfile_impl(ofs.rdbuf());}
FILE* cfile(std::ifstream const& ifs){return cfile_impl(ifs.rdbuf());}

可以使用,

int main(){
    std::ofstream ofs("file.txt");
    fprintf(cfile(ofs), "sample1");
    fflush(cfile(ofs)); // ofs << std::flush; doesn't help 
    ofs << "sample2\n";
}

限制:(欢迎提出意见)

  1. 我发现在fflush打印到fprintf之后std::ofstream非常重要,否则<!>“; sample2 <!>”;出现在<!>“; sample1 <!>”之前;在上面的例子中。我不知道是否有比使用ofs << flush更好的解决方法。值得注意的是std::stringstream无济于事。

  2. 无法从stderr中提取文件*,我甚至不知道是否可以。 (见下文更新)。

  3. 我仍然不知道如何从std::cerr中提取C的fprintf(stderr, "sample"),例如在fprintf(cfile(std::cerr), "sample")中使用,在这样的假设代码中iostringstream

  4. 关于最后一个限制,我发现的唯一解决方法是添加这些重载:

    FILE* cfile(std::ostream const& os){
        if(std::ofstream const* ofsP = dynamic_cast<std::ofstream const*>(&os)) return cfile(*ofsP);
        if(&os == &std::cerr) return stderr;
        if(&os == &std::cout) return stdout;
        if(&os == &std::clog) return stderr;
        if(dynamic_cast<std::ostringstream const*>(&os) != 0){
           throw std::runtime_error("don't know cannot extract FILE pointer from std::ostringstream");
        }
        return 0; // stream not recognized
    }
    FILE* cfile(std::istream const& is){
        if(std::ifstream const* ifsP = dynamic_cast<std::ifstream const*>(&is)) return cfile(*ifsP);
        if(&is == &std::cin) return stdin;
        if(dynamic_cast<std::ostringstream const*>(&is) != 0){
            throw std::runtime_error("don't know how to extract FILE pointer from std::istringstream");
        }
        return 0; // stream not recognized
    }
    

    尝试处理fscanf

    可以使用istreamfmemopen读取cfile,但是如果想要组合C读取,那么每次读取后需要大量的簿记和更新流的输入位置。 C ++ - 读取。我无法将其转换为像上面这样的<=>函数。 (也许<=> 在每次阅读后都会不断更新)。

    // hack to access the protected member of istreambuf that know the current position
    char* access_gptr(std::basic_streambuf<char, std::char_traits<char>>& bs){
        struct access_class : std::basic_streambuf<char, std::char_traits<char>>{
            char* access_gptr() const{return this->gptr();}
        };
        return ((access_class*)(&bs))->access_gptr();
    }
    
    int main(){
        std::istringstream iss("11 22 33");
        // read the C++ way
        int j1; iss >> j1;
        std::cout << j1 << std::endl;
    
        // read the C way
        float j2;
    
        char* buf = access_gptr(*iss.rdbuf()); // get current position
        size_t buf_size = iss.rdbuf()->in_avail(); // get remaining characters
        FILE* file = fmemopen(buf, buf_size, "r"); // open buffer memory as FILE*
        fscanf(file, "%f", &j2); // finally!
        iss.rdbuf()->pubseekoff(ftell(file), iss.cur, iss.in); // update input stream position from current FILE position.
    
        std::cout << "j2 = " << j2 << std::endl;
    
        // read again the C++ way
        int j3; iss >> j3;
        std::cout << "j3 = " << j3 << std::endl;
    }
    

好吧,你可以得到文件描述符 - 我忘记了方法是fd()还是getfd()。 我使用过的实现提供了这样的方法,但我相信语言标准不需要它们 - 标准不应该关心你的平台是否使用fd用于文件。

由此,您可以使用fdopen(fd,mode)来获取文件*。

但是,我认为标准对同步STDIN / cin,STDOUT / cout和STDERR / cerr所需的机制不一定对您可见。因此,如果您同时使用fstream和FILE *,缓冲可能会让您感到困惑。

此外,如果fstream或FILE关闭,它们可能会关闭底层fd,因此您需要确保在关闭之前清空BOTH。

在单线程POSIX应用程序中,您可以通过便携方式轻松获取fd编号:

int fd = dup(0);
close(fd);
// POSIX requires the next opened file descriptor to be fd.
std::fstream file(...);
// now fd has been opened again and is owned by file

如果此代码与其他线程打开文件描述符竞争,则此方法会在多线程应用程序中中断。

另一种在Linux中执行此操作的方法:

#include <stdio.h>
#include <cassert>

template<class STREAM>
struct STDIOAdapter
{
    static FILE* yield(STREAM* stream)
    {
        assert(stream != NULL);

        static cookie_io_functions_t Cookies =
        {
            .read  = NULL,
            .write = cookieWrite,
            .seek  = NULL,
            .close = cookieClose
        };

        return fopencookie(stream, "w", Cookies);
    }

    ssize_t static cookieWrite(void* cookie,
        const char* buf,
        size_t size)
    {
        if(cookie == NULL)
            return -1;

        STREAM* writer = static_cast <STREAM*>(cookie);

        writer->write(buf, size);

        return size;
    }

    int static cookieClose(void* cookie)
    {
         return EOF;
    }
}; // STDIOAdapter

用法,例如:

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/device/file.hpp>

using namespace boost::iostreams;

int main()
{   
    filtering_ostream out;
    out.push(boost::iostreams::bzip2_compressor());
    out.push(file_sink("my_file.txt"));

    FILE* fp = STDIOAdapter<filtering_ostream>::yield(&out);
    assert(fp > 0);

    fputs("Was up, Man", fp);

    fflush (fp);

    fclose(fp);

    return 1;
}

有一种方法可以从fstream获取文件描述符,然后将其转换为FILE*(通过fdopen)。我个人认为在dup2中没有任何需要,但是使用文件描述符你可以做很多有趣的事情,比如重定向(.cpp)。

解决方案:

#define private public
#define protected public
#include <fstream>
#undef private
#undef protected

std::ifstream file("some file");
auto fno = file._M_filebuf._M_file.fd();

最后一个字符串适用于libstdc ++。如果您正在使用其他库,则需要对其进行反向工程。

这个技巧很脏,会暴露fstream的所有私人和公共成员。如果您想在生产代码中使用它,我建议您使用单个函数.h创建单独的int getFdFromFstream(std::basic_ios<char>& fstr);和<=>。头文件不得包含fstream。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top