3D array Question

Started by
3 comments, last by IOStream 21 years, 10 months ago
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.
Advertisement
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]
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.
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.

Thanks brunow, it works. I''ve been using fprintf() all this time. -Appreciate the help.

This topic is closed to new replies.

Advertisement