Jump to content
  • Advertisement

Archived

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

reaptide

Storing an array in a file

This topic is 6508 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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!