English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

plz suggest me a methodology to write a three dimensional array to a file..........i m using gcc as a compiler

2007-07-29 19:37:07 · 2 answers · asked by Akhil T 1 in Computers & Internet Programming & Design

2 answers

It's been a long time since I've done something like this in C.

There is only one good way to my knowledge. If the length of the dimensions change, then you first need to write out the number of dimensions. Then perform a loop within a loop within a loop to write out each element. For example:

int xLength = 10;
int yLength = 10;
int zLength = 10;
char myBigArray[xLength][yLength][zLength];
// Do something to fill the array.
f1 = fopen("myfile.dat", "wt")
for(int x = 0;x < 10;x++)
for(int y = 0;y < 10;y++)
for(int z = 0;z < 10;z++)
{
fprintf(f1, "%d\n", myBigArray[x][y][z]);
}

However, I think that it would be better if you used XML, but I'm not sure how to do that effeciently in C.

2007-07-29 19:56:17 · answer #1 · answered by Michael M 6 · 0 0

arrays are stored contiguously.SO you need not use 3 for loops.Jus open the file and fwrite((void*)name_of_3d_array,sizeof(each_element),total_number_of_elements,fp)

2007-07-29 20:37:13 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers