In C, when opening a file with
FILE *fin;
fin=fopen("file.bin","rb");
I only have a pointer to a structure of FILE. Where is the actual FILE struct allocated on Windows machine? And does it contain all the necessary information for accessing the file?
My aim is to dump the whole data segment to disk and then to reload the dumped file back to the beginning of the data segment. The code that reloads the dumped file is placed in a separate function. This way, the fin pointer is local and is on the stack, thus is not being overwritten on reload. But the FILE struct itself is not local. I take care not to overwrite the memory region of size sizeof(FILE) that starts at the address fin.
The
fread(DataSegStart,1,szTillFin,fin);
fread(dummy,1,sizeof(FILE),fin);
fread(DataSegAfterFin,1,szFinTillEnd,fin);
operations completes successfully, but I get an assertion failure on
fclose(fin)
Do I overwrite some other necessary file data other than in the FILE struct?
The actual instance of the
FILEstructure exists within the standard library. Typically the standard library allocates some number of FILE structures, which may or may not be a fixed number of them. When you callfopen(), it returns a pointer to one of those structures.The data within the
FILEstructure likely contains pointers to other things such as buffers. You’re unlikely to be able to save and restore those structures to disk without some really deep integration with your standard library implementation.You may be interested in something like CryoPID which does process save and restore at a different level.