Binary files on c -
trying save array of structs in binary file. think need for.
typedef struct { int y, o; }num; int main(){ num numbers[10]; (i = 0; < 10; i++){ numbers [i].y = i; numbers [i].o = * 2; } file *f; f=fopen("test","r+"); }
if compatibility different architectures and/or compilers isn't of concern can write data ( in sense write load it). if have have compatiblity can't use method have write functions read , write on more abstract level.
- pass reference data first
fwrite()
, in case pointer numbers - use
sizeof()
function on struct name in 2nd argument, tells function how big data is.sizeof(num)
you - use amount of indeces of array write correct amount file. have 10
10
. - last
file *
see here short example:
typedef struct { int y, o; }num; int main(){ num numbers[10]; (i = 0; < 10; i++) { // blow array bounds, fixed numbers [i].y = i; numbers [i].o = * 2; } file *f; f = fopen("test","r+"); if (f != null) { fwrite(&numbers, sizeof(num), 10, f); fclose(f); } else { // fopen error... } exit(0); }
Comments
Post a Comment