[Mono-list] C# bindings and FILE* question

Jonathan Pryor jonpryor@vt.edu
Mon, 30 Aug 2004 07:54:32 -0400


On Mon, 2004-08-30 at 04:01, Артем Попов wrote:
> Is it possible to pass a Stream object to the C code as FILE* pointer?

No.  They're completely different.  You could P/Invoke into libc for
fopen(3), fclose(3), etc., and use IntPtr as a FILE*, if necessary.

> I'd like to wrap some native code that has lots of functions with FILE*-
> args and I want to pass at least to kind of Stream from the class
> library to them (the Memory Stream and the File Stream)

This isn't possible, even if you were in C.  FILE* doesn't support
polymorphism, so there's no way for you to handle the read/write
requests, so that you could write to a memory buffer instead of a file. 
That's just the limitations of the standard C library, and there's no
way to change it (unless you change the standard to support such
functionality, and wait for the implementations to match the
standard...).

This is one of the reasons why C++ introduced iostreams (that, and to
have type-safe I/O of class objects without needing extra casts).

Consequently, if you want to read data written to a FILE*, you have to
use a temporary file first, and then read the file afterward.

 - Jon