简体   繁体   English

是否可以使用对自定义结构的FILE *起作用的函数?

[英]Is it possible to use functions that acts on FILE* on custom structures?

Very often I see libraries that implements their own stream functionalities, instead of using FILE* . 我经常看到实现自己的流功能的库,而不是使用FILE* The typical interface will have a close function, similar to fclose() , and several open functions, one of which usually mimics fopen() and one of which usually accepts a few callbacks that should be used to open/close the stream, read to/write from the stream. 典型的接口将具有一个类似于fclose()的关闭函数,以及多个打开函数,其中一个通常模仿fopen() ,其中一个通常接受一些应用于打开/关闭流的回调,读取为/从流中写入。

As a reference, good examples of what I am talking about are http://www.xmlsoft.org/xmlio.html or https://developer.gnome.org/gio/ . 作为参考,我正在谈论的很好的例子是http://www.xmlsoft.org/xmlio.htmlhttps://developer.gnome.org/gio/

The approach, in general, seems very straightforward to me, however these libraries do not usually implement a replacement for all the functions in the standard library (eg, fscanf() , fprintf() , ...). 通常,这种方法对我来说似乎非常简单,但是这些库通常不替代标准库中的所有功能(例如, fscanf()fprintf() ,...)。

Thus I wonder if an extension mechanism is available for standard library FILE* as well (eg: opening by providing callbacks for some low-level required functionalities). 因此,我想知道扩展机制是否也可用于标准库FILE* (例如:通过提供一些底层必需功能的回调来打开)。 I was not able to find any reference about this capability, so I guess it is not part of any standard. 我找不到有关此功能的任何参考,因此我想它不是任何标准的一部分。

Anyway, here is my question: is this functionality available in the C standard library (any standard is fine, as long as it is portable)? 无论如何,这是我的问题:C标准库中是否提供此功能(只要可移植,任何标准都可以)? If not is there any easy (ie, it does not require to re-implement the whole stdio.h functions) option that allows to implement it on top of the standard library? 如果没有的话,是否有任何简单的选项(即不需要重新实现整个stdio.h函数)就可以在标准库的顶部实现它呢?

It depends on the C library you're using. 这取决于您使用的C库。 Glibc, for example, supports custom streams through fopencookie ( further documentation here ). 例如,Glibc通过fopencookie支持自定义流( 此处提供更多文档 )。 FreeBSD (and probably other BSDs as well, including OS X) have funopen . FreeBSD(可能还有其他BSD,包括OS X)都具有funopen It doesn't look like Microsoft's C library supports such a feature. 看起来Microsoft的C库似乎不支持这种功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM