Advertisement

Loading A Entire Filfe from disk to MEM

Started by August 06, 2001 05:15 PM
0 comments, last by Zerosignull 23 years, 6 months ago
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~
  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);fclose(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)        do_stuff();    else if(*currentPosition == TILE_TYPE_TWO)        do_other_stuff;}  

Or something like that.

~~~~~~~~~~
Martee
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement