从FILE *指针创建fstream对象 [英] Creating fstream object from a FILE* pointer

查看:118
本文介绍了从FILE *指针创建fstream对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建 fstream 对象的众所周知的方法是:

The well known way of creating an fstream object is:

ifstream fobj("myfile.txt");

即.使用文件名.

但是我想使用文件描述符创建一个ifstream对象.

But I want to create an ifstream object using a file descriptor.

原因::我想使用_popen() 执行命令. _popen()将输出返回为FILE*.因此,其中包含一个FILE *指针,但没有文件名.

Reason: I want to execute a command using _popen(). _popen() returns the output as a FILE*. So there is a FILE* pointer involved but no filename.

推荐答案

您不能仅在标准C ++中执行此操作,因为iostream和C I/O完全独立且不相关.但是,您可以编写由C FILE流支持的 own iostream.我相信GCC附带了一种流类,例如库扩展.

You cannot do that just in standard C++, since iostreams and C I/O are entirely separate and unrelated. You could however write your own iostream that's backed by a C FILE stream. I believe that GCC comes with one such stream class as a library extension.

或者,如果您想要的只是包装C FILE流的一种基于对象的方式,则可以

Alternatively, if all you want is an object-y way of wrapping a C FILE stream, you could use a unique pointer for that purpose.

这篇关于从FILE *指针创建fstream对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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