Posted by: |<1$|-| July 14, 2007
Login in to Rate this Post:
0
?
Here is a sample code to write a 2D array of double to a file and read it back. Things work similarly for higher dimensions and dynamically assigned arrays.
#include
#include
int main()
{
FILE * opfile, * readfile;
double var[5][5], varr[5][5];
int i,j,k;
for(i = 0; i < 5; i++)
for(j = 0; j < 5; j++)
var[i][j] = i+j+1;
opfile = fopen("storefile", "w");
if(opfile == NULL){
printf("Oops!\n");
return 0;
}
fwrite(var, sizeof(double), 25, opfile);
fclose(opfile);
readfile = fopen("storefile", "r");
fread(varr, sizeof(double), 25, opfile);
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j ++)
printf("%f %f\n", varr[i][j], var[i][j]);
}
return 0;
}