how to create a FILE * that points to memory

Posted on 2006-03-20
Last Modified: 2007-12-19

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");
Question by:vertico
    LVL 24

    Expert Comment

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


    Author Comment

    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

    Author Comment

    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
    LVL 16

    Accepted Solution

    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. 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.


    Author Comment

    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
    LVL 16

    Expert Comment

    Excellent! And thanks for the points :-)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now