Assigning an array to a FILE structure

Stephen Clamage steve at taumet.com
Fri Oct 12 01:39:07 AEST 1990


tomr at ashtate (Tom Rombouts) writes:

>They say the only stupid question is the one that isn't asked, so:

>Is it possible using the ANSI stream I/O functions to assign an
>array to a FILE structure so that fgetc() etc. will work on it?

There is no portable way to do this in ANSI C.  You could assign
your own buffer to the FILE structure, and figure out how to set the
related pointer and size fields of the structure to correspond.  But the
stdio library is still going to assume an open file somewhere, and try to
use low-level read calls to get more data -- this won't work, of course.

You can do this easily in C++, since the stream (and iostream) library
explicitly supports using a character array as if it were a file --
all the I/O and EOF operations work correctly with no attention
from the programmer.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list