• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.