c - Fwrite doesn't work properly -


sorry having program in native language, can't seem find why doesn't work. so, tested , values of array correctly read, when try @ .dat file there first word read in function ( a[0].marca ).

here input i tested see if reads correct

here .dat file it writes first

#include <stdio.h> #include <stdlib.h>  struct data {     int anul;     int luna; }; typedef struct data data; struct automobil {     char marca[20];     char carburant;     char model[5];     data fabricatie; }; typedef struct automobil automobil;     int main() {     automobil a[100];     int n;     file *f;      int i;     if((f=fopen("evidenta.dat","wb"))==null)     {         exit(1);     }     printf("cate automobile sunt ?"); scanf("%d",&n);  // number of cars registered     for(i=0;i<n;i++)   // getting details every car     {         printf("\nmarca ? : "); fflush(stdin); gets(a[i].marca);         printf("\ncarburant ? : "); fflush(stdin); getch(a[i].carburant);         printf("\nmodelul? :");  fflush(stdin); gets(a[i].model);         printf("\nluna fabricatie ? :"); scanf("%d",&a[i].fabricatie.luna);         printf("\nan fabricatie ? : "); scanf("%d",&a[i].fabricatie.anul);          // after getting line has write in binary file         fwrite(&(a[i]),sizeof(automobil),1,f); //it writes  a[0].marca     }      for(i=0;i<n;i++) {     printf("\n %s",a[i].marca);     printf("\n %c",a[i].carburant);     printf("\n %s",a[i].model);     printf("\n %d",a[i].fabricatie.luna);      printf("\n %d",a[i].fabricatie.anul); }      return 0;  } 

you have way:

fwrite(&(a[i]),sizeof(automobil),1,f); 

alternatively can outside loop:

fwrite(a,sizeof(automobil),n,f); 

also don't forget fclose.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -