?
Solved

how to create a FILE * that points to memory

Posted on 2006-03-20
6
Medium Priority
?
475 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");
}
0
Comment
Question by:vertico
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 16236562
Sounds to me as if you like to look up memory mapped files.

Regards
Friedrich
0
 

Author Comment

by:vertico
ID: 16238858
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
0
 

Author Comment

by:vertico
ID: 16238905
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Accepted Solution

by:
PaulCaswell earned 2000 total points
ID: 16243180
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
0
 

Author Comment

by:vertico
ID: 16247734
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
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16247781
Excellent! And thanks for the points :-)

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

862 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