I have a piece of code that needs to be run from a restricted environment that doesn’t allow stdio (Flash’s Alchemy compiler). The code uses standard fopen/fread functions and I need to convert it to read from a char* array. Any ideas on how to best approach this? Does a wrapper exist or some library that would help?
Thanks!
EDIT: I should also mention that it’s reading in structs. Like this:
fread(&myStruct, 1, sizeof(myStruct), f);
I don’t know of any such wrapper, but I don’t think it would be too difficult to make your own. That’s because C’s approach to file I/O hides everything behind the
FILE*interface, which actually makes it nicely object-oriented.Since you’re using C rather than C++, I would suggest using preprocessor macros to replace every instance of
fopen(),fclose()andfread()withMEM_fopen()etc. which are routines that you will define. You will need to define your ownFILEtype, for which you could simply use the following:(If you need to manage EOF, you will instead need
FILEto be astructwith an additionallengthfield.)Then your
MEM_fread()function will look something like:The signature for the
MEM_fopen()“constructor” may need to change slightly, since the identifier you need is now a memory address instead of a filename.