C: Reading strings from binary file -


i have home assignment says have store information mansion in binary file. on first line of file have keep information how many floors there in building , how many apartments there on floor (size of two-dimensional array). , on next lines have keep info apartments - unique number each apartment, number of rooms in apartment, number of people living there, family name, date when family moved in apartment, monthly rent.

so, question is: how should store information , how should read it? if use fread(buf, size, number_of_elements, file *), how know number of elements? example, how read family name when don't know it's lenght?

thanks in advance!

ps. thank you, claptrap! tried write data first binary file got error :(

#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h>  #define napartments 21 #define tax_adult 3 #define tax_kid 1 #define tax_elevator 2 #define nfloors 7 #define nrooms 3  void main() {     file *fp;     int i, j;      struct info_apartments     {         unsigned numapartment, numrooms, numadults, numkids, rent;         char family[30], date[10];     };     typedef struct info_apartments data;      data apartments[7][3];      for(i=0; i<nfloors; i++)         for(j=0; j<nrooms; j++)         {             apartments[i][j].numapartment=i*10+j+1;             printf("\nenter number of rooms: ");             scanf("%d", apartments[i][j].numrooms);             printf("\nenter number of adults: ");             scanf("%d", apartments[i][j].numadults);             printf("\nenter number of kids: ");             scanf("%d", apartments[i][j].numkids);             printf("\nenter family name: ");             scanf("%s", apartments[i][j].family);             printf("\nenter date: ");             scanf("%s", apartments[i][j].date);             //first 2 floors don't pay elevator             if(i+1<3)                 apartments[i][j].rent=(tax_adult*apartments[i][j].numadults)+(tax_kid*apartments[i][j].numkids);             else                 apartments[i][j].rent=((tax_adult+tax_elevator)*apartments[i][j].numadults)+((tax_kid+tax_elevator)*apartments[i][j].rent);         }     if((fp=fopen("myfile", "ab"))==null)     {         printf("error");         exit(1);     }      if((fwrite(apartments, sizeof(apartments), 1, fp))!=1)     {         printf("error!\n");         exit(1);     } } 

consider creating structs holding information need store/retrieve. header struct like

typedef struct {   short floors;   short apartments; } header; 

you more structs other parts of file.

then when read (frwrite write) file like

header h; fread( &h, sizeof(h), 1, fp ); 

now using information in header can calculate how read/write rest of file. rest of file have other structs leave solve.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -