Skip to main content
gamedev.net
🔒 Locked

Memory pointer cast to FILE* ?

Started by MatrixCubed 5 replies 1.2k views
Original Post
MatrixCubed
MatrixCubed
Hello all, Is there a method of converting a memory-buffer pointer (e.g. char*) to a stdio FILE* pointer? Is "file/pipe redirection" what I'm looking for? I've come across a few *nix sites on how to do something similar from the commandline, but I'm really looking for source examples. Thanks, - m³
SiCrane
SiCrane
Not in standard C. However, there's not much reason to as you can use in-memory versions of all the file I/O functions. ex: sscanf() instead of fscanf().
griffin2000
griffin2000
The way I implement this is to write your own file API that when in "real" file mode can read off disk directly (in this case it just maps directly to the windows file IO functions), but that can also be used to read from RAM-disks. E.g.:

MyFileHdl file1,file2;file1=MyFile::Fopen("file.txt",flags);//Maps to fopen or another suitable windows file APifile2=MyFile::Fopen(pBuffer,bufferSize);//Maps to my own simple ram disk libraryfile1->Read(pPtr,12); //Calls freadfile2->Read(pPtr,12); //Calls memcpy and increments internal pointer to buffer
Zahlman
Zahlman
Dear god no.

What are you *really* trying to do?
griffin2000
griffin2000
Quote:
Original post by Zahlman
Dear god no.


Not quite sure what so terrible about that solution :) As I said most of the projects I've worked on have used this solution (or a C-based version of it). It has many advantages (it allows abstraction of the file system to allow for platform differences, and for support for pak/zip files without changing your code). There is a virtual function overhead, but thats generally pretty trivial compared to the file system access time. It also allows for an easy to use wrapper around ascynchronous file calls.
ToohrVyk
ToohrVyk
Quote:
Original post by griffin2000
The way I implement this is to write your own file API that when in "real" file mode can read off disk directly (in this case it just maps directly to the windows file IO functions), but that can also be used to read from RAM-disks. E.g.:

*** Source Snippet Removed ***


Your code looks suspiciously like C++. The standard library of the C++ language contains iostreams, which already provide what you need (see the fstream and sstream headers). Do you have any reasons to reimplement this?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.