• C

how to create a FILE * that points to memory


I want to create a FILE pointer (FILE *) that points to an array of bytes
the reason is I must extract a file from a zip file, and pass it to an image library that takes FILE * as parameters
I want to avoid temporary files

is there any other way then using pipes ?
here's what I've done with pipes

int tmppipe[2];
int len, offset = 0;
FILE *fp = NULL;

if (_pipe(tmppipe, bufferSize, _O_BINARY) == 0)
            len = _write(tmppipe[1], buffer + offset, bufferSize - offset);
            offset += len;
      while (offset < bufferSize);


      fp = _fdopen(tmppipe[0], "rb");
Who is Participating?
Hi vertico,

The FILE struct is implementation dependant and therefore not the most portable method. However, if that is what you need, here's one way you could try it:

Here's the definition of a FILE struct from my stdio.h (Visual C++ 2005):

struct _iobuf {
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
typedef struct _iobuf FILE;

I am guessing here. Use a good debugger to see what a fread does to the structure to confirm.

Open a normal file for read only - fopen ("tempfile","rb");
_base will point to a buffer used to hold the data. Replace this with a pointer to your buffer. Retain the original because you will need to put it back before closing the file.
_cnt is probably the number of bytes remaining in the buffer. Set this to the number of bytes in your buffer.
_ptr.is probably the the pointer to the next byte to read. Point this to your buffer too.
_bufsiz is probably the real size of your buffer, set this the same as _cnt.

Not sure what the others are likely to do. You're probably better off leavingthem alone unless you really need them to change. Remember to put the data back as it was before closing.

Some of this functionality may already be available with the setvbuf function if it is available in your libraries.

Sounds to me as if you like to look up memory mapped files.

verticoAuthor Commented:
from what I read on the subject, memory mapped files does not suit my needs
with memory mapped files, you load a file into memory and access it the same way you access dynamic memory, trough pointers.

I have an array of bytes (unsigned char *) but I want to access it like a file through a FILE structure
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

verticoAuthor Commented:
does any body has good documentation on the FILE struct
maybe I can set the members point at my buffer and fool read file reading functions like: fread fscanf
verticoAuthor Commented:
this is what I found from debugging fopen() and fread() with Microsoft Visual Studio 2003

FILE file;
file._charbuf = NULL;
file._file = -1;
file._tmpfname = NULL;
file._flag = _IOMYBUF | _IOREAD | _IOSTRG;
file._base = file._ptr = (char *)buffer;
file._bufsiz = bufferSize;
file._cnt = bufferSize;

flags :
_IOMYBUF: tells that a buffer had been allocated
_IOREAD: tells the stream is opened
_IOSTRG: ensures me that the buffer won’t be freed in fclose

all the functions I tested worked well

since this code will probably never migrate to an other platform, I guess it's safe to use
Excellent! And thanks for the points :-)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.