Link to home
Start Free TrialLog in
Avatar of gaggio
gaggio

asked on

How to bind a buffer to a FILE pointer

I am programming in VC6.

I would like to bind a buffer to a FILE pointer in order to be able to use fopen(), fread(), fwrite(), fseek() and fclose(). I know it may seem a little weird but I have a good algorithm that works with a FILE* and that I would not like to rewrite to make it work with a buffer.

I would like to know if somebody knows a C++ class or a set of C functions to do this.

I have looked a mmap() kind of functions but they do not work since they aren't Windows compatible.

Thanks for your help!
Avatar of ozo
ozo
Flag of United States of America image

ostream(streambuf*)
strstream()
create yourself a new class with the fopen(), fread(), fwrite(), fseek() and fclose() methods

implement it with a strstream as the buffer, or any other buffer type you like.

send in a pointer to an instance of your class to your manificient algorithm ;)

your probably have to make your algorithm template based (the FILE* argument is a template instead)

hth
Avatar of gaggio
gaggio

ASKER

hmmm, thanks balder but this is still a little bit unclear since there is no direct bridge AFAIK between FILE* and C++ streams, is there?
no, but as I understood from your question, the only bridge needed is the use of the functions fopen(), fread() etc.

when you have a templatized function that uses this functions on the template parameter you don't need more than a pointer to an object with theese functions

you will have to change your algorthm slightly, but only in the function declaration unless you have declared some FILE* in the function.

post your function declaration and I will give an example
>>I have looked a mmap() kind of functions but they do not work since they aren't Windows compatible.

mmap is a file mapping API function for UNIX/Linux.
Windows also has similar file mapping functions, and it's not that hard to code so that you're code will work with both Windows and UNIX/Linux.

For Windows, look at the following API functions:
CreateFileMapping
MapViewOfFile

continue....
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I would like to bind a buffer to a FILE pointer in order to be able to use fopen(), fread(), fwrite(), fseek() and fclose()

'setvbuf()' serves exactly that purpose:

char* buffer = new char[MY_BUFFER_SIZE];

FILE* p = fopen("myfile.txt","r");

setvbuf(p,buffer,IOFBF,MY_BUFFER_SIZE);
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I doubt that POSIX functions like fread, fwrite, fseek can be used on a buffer rather than on a file.

You can however use POSIX memory map functions like mmap.  For windows, you can use the FileMap API functions to accomplish the same thing.
>>If I understood the questioner correctly he has no file but only a buffer and wants to operate on that buffer

If so, I've indeed understood that the wrong way.
You know, I've never heard of anyone simulating a FILE structure just within the project.  

corey
Avatar of gaggio

ASKER

Apparently, there is no way of really doing what I would like.
But Axter thanks a lot for your code/reply.
Itsmeandnobodyelse: I will probably go "the file creation" way, that is I will create a file with the buffer and then use the algo and that file. It's simple even if not very efficient when the buffer is big.
setvbuf has *nothing* to do with my question: it's good to read the question well before answering...