Saving and Reading struct array with binary file in C language -
there problems reading function.
my reading function is:
int readfiletopic(file* f, pic* picture_reccord, int picnumber){   int count = 0;   f = fopen("record.dat", "rb");   if (f == null){     printf("\n unable open file!\n");  }  else{     count= fread(&picture_reccord, sizeof(picture_reccord), maximun_picture,    f);     fclose(f);  }  if (count <=0 && count >= maximun_picture)     return -1;    //breaking programe   //picture_reccord[count].filename[0] = '\n';   return count;   } this how call
case 3:     printf("read picture records disk\n");     count= readfiletopic(file, picturerecord, picnumber);     printf("\n\nread %d photos\n", count);     //testing     printf("\n%d\n", picturerecord[0].location);     break; it prints "read 4 photos" every time , grabage in testing part
this saving function in case problems are
void savepic(file* f, pic picture_record){  f= fopen("record.dat","wb"); if (f == null) {     printf("\nerror! not able save!\n"); }  fwrite(&picture_record, sizeof(picture_record), 1, f); fclose(f);  printf("one pic saved\n"); }
this updated struct @ jonathan leffler
typedef struct picture_data { char filename[input_length_filename]; char description[input_length_description]; char location[input_length_filename]; int peoplecount; }pic; can me , tell me problems ?
when structure contains pointers, cannot 'serialized' sensibly.  is, cannot written using of functions fwrite().
why not, ask? question!
think of way: if have structure containing pointer string (a name, example), data in string not contiguous structure, , size isn't included in size of structure.
by contrast, if structure contains (fixed size) array of characters, can write whole structure because data contiguous rest of structure, , counted in size of array.
flexible array members require little bit of care, can written disk provided main structure records how big flexible array is. can write in 1 operation, need read in two. first read reads inflexible part of structure; 1 of data items in tells how space allocate , how data read.
Comments
Post a Comment