Storing an array in a file

Started by
4 comments, last by reaptide 23 years, 4 months ago
Hello everyone, I have to admit that I am clueless when it comes to files. I need to store the data for my map in a file for later use. The problem is that I don''t know how to store a 2d array in a file. Below is the source that I am using right now. It doesn''t work at all. Can someone give me a bit of help here. // BEGIN SOURCE //////////////////////////////////////////////// int Map[20][15]; // Filled with data earlier in the prog. BOOL SaveMap(LPSTR Filename) { ofstream Mapfile; Mapfile.open(Filename, ios::out); Mapfile << Map; Mapfile.close(); return TRUE; } // END SOURCE ////////////////////////////////////////////////// Thanks for any help, Derek Sauer
Advertisement
I've always stuck with fread and fwrite. They are easy to remember and to use.

fread (pointer to item, size of item, number of items, file)

fwrite (pointer to item, size of item, number of items, file)


    bool save(char* fileName){        FILE* file;	//open file to write as binary        file = fopen(fileName, "wb"); 	// Write the dimensions of the map ( needed for loading )        fwrite(&mapSizeX, sizeof(int), 1, file);        fwrite(&mapSizeY, sizeof(int), 1, file);                	//Write all the elements individually               for ( int y = 0; y < mapSizeY; y++ )                for ( int x = 0; x < mapSizeX; x++ )                        fwrite(&Map[x][y], sizeof(int), 1, file);         fclose(file);          return true;}    



---------
Andrew

Edited by - acraig on November 24, 2000 2:01:56 PM
If you are using C file I/O, I think you''d be better off writing your 2D array in one go.

i.e.

fwrite(Map,sizeof(int),sizeof(Map)/sizeof(int),file);

But, if you do it item-by-item as Andrew suggests, you should swap the array index variables (x and y) around.

i.e.

fwrite(&Map[y][x], sizeof(int), 1, file);

Doing it the other way will not write the array out sequentially. Remember, 2D arrays are stored in row major order, so the first index is the row.

This would not be a problem if you wanted to read the array back in using the same procedure.
"fwrite(Map,sizeof(int),sizeof(Map)/sizeof(int),file);"

Shouldn''t it be something like

fwrite(Map,sizeof(MAP_STRUCTURE)*MaxSizeX*MapSizeY,1,filep);

Here, Map has to be a pointer to the map... (duh)
and MAP_STRUCTURE is the struct for each part of the map
(At declaration: MAP_STRUCTURE map[MapSizeX][MapSizeY];
And MapSizeX/Y are #defines... naturally... )

hell, i dont know... but i think thats correct

That would just save all info in the order the data apear... i think you could use the same method to load the map...

=======================
Game project(s):
www.fiend.cjb.net
=======================Game project(s):www.fiend.cjb.net
quote:Original post by JonatanHedborg

"fwrite(Map,sizeof(int),sizeof(Map)/sizeof(int),file);"

Shouldn''t it be something like

fwrite(Map,sizeof(MAP_STRUCTURE)*MaxSizeX*MapSizeY,1,filep);

Here, Map has to be a pointer to the map... (duh)
and MAP_STRUCTURE is the struct for each part of the map
(At declaration: MAP_STRUCTURE map[MapSizeX][MapSizeY];
And MapSizeX/Y are #defines... naturally... )



Nope. Since Map was not declared as a pointer sizeof() will return the size (in bytes) of the array. Just try it:

int temp[80][80];
cout << sizeof(temp);

will return 25600.



------------
Andrew

For me,I will write the function as the following:
      BOOL ReadWriteMapFile(LPSTR lpszFileName,int *pMap,int nXVal,                      int nYVal, BOOL bWrite){ fstream  fsMapFile; int       nx,ny;   if (bWrite)    fsMapFile.open(lpszFileName, ios::in); else    fsMapFile.open(lpszFileName, ios::out);  for(nx=0;nx<nXVAL;++nx)    for  (ny=0;ny<nYVAL;++ny)      {        if (bWrite) //write the map data into the specificing file           {             fsMapFile<<pMap[nx][ny];                       fsMapFile<<' ';           }        else  //read the map data from the specificing file           fsMapFile>>pMap[nx][ny];                 }  fsMapFile.close();  return TRUE;}[/source]and then, I will use the above function as the following:[source] int   Map[20][15]; LPSTR lpszMapFile[]={"map.dat"}; //the other program line here ReadWriteMapFile(lpszMapFile,Map,20,15,TRUE); //write the data into the file ReadWriteMapFile(lpszMapFile,Map,20,15,FALSE); //read the data from  the file //the other program line here     




Edited by - zhang_zhou on November 27, 2000 12:49:32 AM

Edited by - zhang_zhou on November 27, 2000 12:53:17 AM
============================= Hey,I just wanna know WHY! =============================

This topic is closed to new replies.

Advertisement