Archived

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

3D array Question

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

How would you go about loading and saving a 3D array into a custom external file (say, file.dat or something)? I tried hours of experimentation using fscanf() and other c++ func''s to load and save one, and all i figured out was how to make a mock-up external file using putc(''\n'',file) to test loading it. Still cant load it right and obviously cant save one.

Share this post


Link to post
Share on other sites
say your array is declared like that :

int array[10][20][30];

To save your array, you can do :

FILE *file;
file = fopen("filename.bin","wb");
if (!file) return error_code; // return your own err codes format
fwrite(array,sizeof(int),10*20*30,file);
fclose(file);

to load it, easy to guess :

FILE *file;
file = fopen("filename.bin","rb");
if (!file) return error_code; // return your own err codes format
fread(array,sizeof(int),10*20*30,file);
fclose(file);

Hope it helps


Exood4 Studios
http://www.exood4.com

[edited by - brunow on June 23, 2002 8:46:13 PM]

Share this post


Link to post
Share on other sites
im only a neewbie. ive heard and used

file = fopen("filename.bin","w"); and
file = fopen("filename.bin","r"); but what does

file = fopen("filename.bin","wb"); and
file = fopen("filename.bin","rb"); do, or is it the same thing.

Share this post


Link to post
Share on other sites
In Windows, they actually do the exact same thing.

...elsewhere, the b stands for "binary" and opens up the file as a binary file instead of a text file, allowing you to manipulate it byte by byte, instead of reading lines and strings.

Share this post


Link to post
Share on other sites