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.

  1. pass reference data first fwrite(), in case pointer numbers
  2. use sizeof() function on struct name in 2nd argument, tells function how big data is. sizeof(num) you
  3. use amount of indeces of array write correct amount file. have 10 10.
  4. 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

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -