strstream()
Main Topics
Browse All TopicsI 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!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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....
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(hFileHan
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(hMapF
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(SearchDa
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;
}
For example code on how to use file mapping in a portable way, see following links:
http://code.axter.com/shar
http://code.axter.com/Shar
http://code.axter.com/file
http://code.axter.com/file
>>>> '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
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...
Business Accounts
Answer for Membership
by: ozoPosted on 2005-07-05 at 02:01:57ID: 14366871
ostream(streambuf*)