This topic is now archived and is closed to further replies.


Loading A Entire Filfe from disk to MEM

Recommended Posts

I was just wondering how i would go about reading a file ie a map file into memory and then working on it from memory instead of disk ~prevail by daring to fail~

Share this post

Link to post
Share on other sites
FILE* inFile = fopen("map.txt", rb);
int length = 0;
int startPosition;
char* fileData;
startPosition = ftell(inFile);
fseek(inFile, 0, SEEK_END);
length = ftell(inFile);
fseek(inFile, 0, startPosition);
fileData = (char*)malloc(length * sizeof(char));
fread(fileData, 1, length, inFile);

I think that should do it. Now, a copy of the data contained in the file is stored in memory, and fileData is a pointer to it. So to use it, you would do something like:
char* currentPosition = fileData;
while(*currentPosition++ != NULL)
if(*currentPosition == TILE_TYPE_ONE)
else if(*currentPosition == TILE_TYPE_TWO)

Or something like that.


Share this post

Link to post
Share on other sites