[VIEWED 4742
TIMES]
|
SAVE! for ease of future access.
|
|
|
chanaa_tarkaari
Please log in to subscribe to chanaa_tarkaari's postings.
Posted on 07-13-07 6:04
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
I know there are many programming experts here in Sajha, I am going to put here a simple problem I faced today seeking your suggestions. I am trying to write a numeric array into a binary file using fwrite. The variable is three dimensional converted into two dimensional double type, var[k][t], where [k] contains two-dimension inside. k is not sequential but has to be picked up from i and j released by separate routine. I coded fwrite(var[k][t], sizeof(var[0][t]), sizeof(var[k][t]), FILE*); but it displayed the following error message, error: cannot convert `double' to `const void*' for argument `1' to `size_t fwrite(const void*, size_t, size_t, FILE*)' I have seen fwrite is used successfully in writing character arrays. How can I use this to write numeric array? It seems that I need to add something to successfully convert 'double' to 'const void*', but did not get any clue on that. Any help or suggestions are welcome.
|
|
|
|
chanaa_tarkaari
Please log in to subscribe to chanaa_tarkaari's postings.
Posted on 07-13-07 10:08
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
!!!
|
|
|
thapap
Please log in to subscribe to thapap's postings.
Posted on 07-14-07 1:51
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
cast the return of the double.. it might work or barf for some other reason later.. but u will b able to pass this one.. (const void * (var [k][t]) ) this should do it.. for ur compilation
|
|
|
|<1$|-|
Please log in to subscribe to |<1$|-|'s postings.
Posted on 07-14-07 4:16
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
From the error message and your code fragment it seems that var is declared as a 2 dimensional array (look at the error message, it says cannot convert 'double' instead of 'double *' => var[k][t] is double instead of double *). The first argument to fwrite() has to be a pointer. You can use type casting but make sure that the pointer points to contiguous block of memory (might not be contiguous if you dynamically allocate 2 or higher dimensional arrays). The third argument to fwrite() in your code fragment seems to have some problems. Although it is correct syntactically, it might have some semantic issues. sizeof(var[k][t]) is sizeof(double) if var is 2D or sizeof(double *) if var is 3D. The point is that it will always be constant (unless this is what you want).
|
|
|
chanaa_tarkaari
Please log in to subscribe to chanaa_tarkaari's postings.
Posted on 07-14-07 9:23
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Thanks Thapap and |<1$|-| for valuable suggestions. Thapap's idea worked out to solve the compiling error message when I added const void * (var [k][t]) ; just before the fwrite statement. But it produced segmentation fault while running the program. It is true the var[k][t] is declared as two dimensional double. The memory allocation is dynamic. Declaring const void*(var[k][t]) in the beginning of program caused further problem of conflict in declaration, with the following error message. error: conflicting declaration 'const void*var[((long unsigned int)((long int)k))][((long unsigned int)((long int)t))]' All examples I could find on fwrite illustrate just writing character type data array. Could not find any good referece to write the numeric data array in binary form,
|
|
|
|<1$|-|
Please log in to subscribe to |<1$|-|'s postings.
Posted on 07-14-07 1:12
PM
Reply
[Subscribe]
|
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; }
|
|
|
|<1$|-|
Please log in to subscribe to |<1$|-|'s postings.
Posted on 07-14-07 1:20
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
heres the code again: #include <stdio.h> #include <stdlib.h> 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("You suck\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; }
|
|
|
lootekukur
Please log in to subscribe to lootekukur's postings.
Posted on 07-14-07 1:50
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
looks like ^ bro overtook me hehe. but since i put my 5 mins into it, here's how i would write numeric values to a binary file :P ======== #include #define size 3 int main () { FILE * pFile; struct var{ double x1; double x2; }; typedef struct var var; var vararr[size]={{3.4,4.5},{5.4,6.1},{7.1,8.9}}; pFile = fopen ( "file.bin" , "wb" ); fwrite (vararr , sizeof(var) , size , pFile ); fclose (pFile); return 0; } ======
|
|
|
chanaa_tarkaari
Please log in to subscribe to chanaa_tarkaari's postings.
Posted on 07-15-07 9:41
AM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Thanks |<1$|-| and Loote for sample codes. Sajha members rock.
|
|