We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

how to create a FILE * that points to memory

vertico
vertico asked
on
Medium Priority
511 Views
Last Modified: 2007-12-19
Hi

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)
{
      do
      {
            len = _write(tmppipe[1], buffer + offset, bufferSize - offset);
            offset += len;
      }
      while (offset < bufferSize);

      _close(tmppipe[1]);

      fp = _fdopen(tmppipe[0], "rb");
}
Comment
Watch Question

fridomCEO/Programmer
CERTIFIED EXPERT

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

Regards
Friedrich

Author

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

Author

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

Paul

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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 :-)

Paul
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.