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!
LVL 1
gaggioAsked:
Who is Participating?
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.

ozoCommented:
ostream(streambuf*)
ozoCommented:
strstream()
balderCommented:
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gaggioAuthor Commented:
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?
balderCommented:
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
AxterCommented:
>>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....
AxterCommented:
Here's some example code for finding a string in a file via file mapping:

#include <iostream>
#include <algorithm>
#include <windows.h>

using namespace std;

int main()
{
     const char FileName[] = "c:\\testfile.txt";
     HANDLE hFileHandle = CreateFile(FileName,
          GENERIC_READ,
          FILE_SHARE_READ,
          NULL,
          OPEN_EXISTING,
          FILE_ATTRIBUTE_NORMAL,
          NULL);
     if (hFileHandle == NULL) {
          printf("Could not open file.\n");
          return -1;
     }
     DWORD FileSize = GetFileSize(hFileHandle, NULL);

     
     HANDLE hMapFile = CreateFileMapping(hFileHandle,    // Current file handle.
          NULL,                              // Default security.
          PAGE_READONLY,                    // Read permission.
          0,                                 // Max. object size.
          0,                                 // Size of hFile.
          NULL);            // Name of mapping object.
     
     if (hMapFile == NULL) {
          printf("Could not create file-mapping object.\n");
          return -1;
     }
     
     const char* lpMapAddress = (char*)MapViewOfFile(hMapFile, // Handle to mapping object.  
          FILE_MAP_READ,               // Read permission
          0,                                 // Max. object size.
          0,                                 // Size of hFile.
          0);                                // Map entire file.
     
     if (lpMapAddress == NULL) {
          printf("Could not map view of file.\n");
          return -1;
     }

     const char SearchData[] = "string";
     const char* FoundData = std::search(lpMapAddress, lpMapAddress+FileSize, SearchData, SearchData+strlen(SearchData));
     if (FoundData == (lpMapAddress+FileSize)) {
          printf("Could not find string.\n");
          return -1;
     }
     int FoundPosition = FoundData-lpMapAddress;

     cout << "Found item at position " << FoundPosition << endl;
     
     
     return 0;
}

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
AxterCommented:
jkrCommented:
>>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);
itsmeandnobodyelseCommented:
>>>> 'setvbuf()' serves exactly that purpose:

setvbuf supplies a user-defined buffer to an opened - but not yet written or read - file.

If I understood the questioner correctly he has no file but only a buffer and wants to operate on that buffer using fread, fwrite, fseek ...

That would be similar to the relation of stringstream class to iostream or fstream classes.

I doubt that POSIX functions like fread, fwrite, fseek can be used on a buffer rather than on a file. The problem is that these functions/structs are C functions/structs that cannot be overloaded as it was possible withe the iostream (class) interface. Of course, you could write the buffer to a file, then read it again and call file streaming functions. If doing that on a RAM disk it would be fast but I don't believe it is worth the efforts.

Maybe you should post the 'algorithms' and interface you have. It is most likely you could easily convert these to iostream interface.

Regards, Alex
AxterCommented:
>>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.
jkrCommented:
>>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.
_corey_Commented:
You know, I've never heard of anyone simulating a FILE structure just within the project.  

corey
gaggioAuthor Commented:
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...
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
C++

From novice to tech pro — start learning today.