获取文件*从一个std :: fstream的 [英] Getting a FILE* from a std::fstream

查看:255
本文介绍了获取文件*从一个std :: fstream的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个(跨平台)的方式获得从C A C FILE *手柄++的std :: fstream的?

Is there a (cross-platform) way to get a C FILE* handle from a C++ std::fstream ?

我想问的原因是因为我的C ++库接受fstreams,在一个特定的功能,我想用一个接受文件中的C库*。

The reason I ask is because my C++ library accepts fstreams and in one particular function I'd like to use a C library that accepts a FILE*.

推荐答案

简短的答案是否定的。

究其原因,是因为的std :: fstream的不需要使用 FILE * 作为其一部分它的实现。所以,即使你设法从中提取的std :: fstream的对象文件描述符和手动构建一个文件对象,那么你就会有其他的问题,因为你现在有两个缓冲对象写入同一文件描述符。

The reason, is because the std::fstream is not required to use a FILE* as part of its implementation. So even if you manage to extract file descriptor from the std::fstream object and manually build a FILE object, then you will have other problems because you will now have two buffered objects writing to the same file descriptor.

真正的问题是,为什么要在的std :: fstream的对象转换为 FILE *

The real question is why do you want to convert the std::fstream object into a FILE*?

虽然我不建议这样做,你可以尝试查找 funopen()。结果
不幸的是,不是一个POSIX API(这是一个BSD扩展),所以它的便携性问题。这也可能是为什么我找不到任何人说已结束一个的std ::流像这样的对象。

Though I don't recommend it, you could try looking up funopen().
Unfortunately, this is not a POSIX API (it's a BSD extension) so its portability is in question. Which is also probably why I can't find anybody that has wrapped a std::stream with an object like this.

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 *)
             );

这允许你建立一个文件对象,并指定将被用来做实际工作的一些功能。如果你写相应的功能,你可以让他们从实际打开了该文件中的的std :: fstream的对象读取。

This allows you to build a FILE object and specify some functions that will be used to do the actual work. If you write appropriate functions you can get them to read from the std::fstream object that actually has the file open.

这篇关于获取文件*从一个std :: fstream的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆