Friday, July 31, 2009

How to write a 3dimensional array to a file in c?

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

How to write a 3dimensional array to a file in c?
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 %26lt; 10;x++)


for(int y = 0;y %26lt; 10;y++)


for(int z = 0;z %26lt; 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.
Reply:arrays are stored contiguously.SO you need not use 3 for loops.Jus open the file and fwrite((void*)name_of_3d_array,sizeof(ea...


No comments:

Post a Comment