Jump to content
  • Advertisement


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


Loading A Entire Filfe from disk to MEM

This topic is 6162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!