Hi Syazzz
here an example to write ONE set of details:
Code:
typedef struct details
{
char plateNo[50];
char vehicleType[50];
char carBrand[50];
int date;
int driverID;
char ticketNo[50];
}details;
void clpuf(void)
{
while (getc(stdin) != '\n')
;
}
int main()
{
details b;
char datname[100] = {0};
FILE *fpwrite,* fpread;
int watu = 21;
strcpy(datname,"data_test.txt");
do
switch (watu)
{
default:
printf("\n---structreadrest\n");
printf("\nEnd of Program............0\n");
printf("\nread file.................2\n");
scanf("%d", & watu);
clpuf();
break;
case 0:
printf("\nEnd of Program\n");
return 0;
break;
case 1:
//newEntry :
printf("\n---New Details---");
printf("\nPlate No. : ");
scanf("%[^\n]", b.plateNo);
clpuf();
printf("\nVehicle Type : ");
scanf("%[^\n]", b.vehicleType);
clpuf();
printf("\nCar Brand : ");
scanf("%[^\n]", b.carBrand);
clpuf();
printf("\nDate : (example: DD/MM/YY) \n");
scanf("%d", &b.date);
clpuf();
printf("\nDriver ID : ");
scanf("%d", &b.driverID);
clpuf();
printf("\nTicket Issued : ");
scanf("%[^\n]", b.ticketNo);
clpuf();
printf("\nTry to create %s\n", datname);
fpwrite = fopen (datname, "w");
if( fpwrite == NULL )
{
printf("Error! File not found");
return(0);
}
fwrite(&b,sizeof(struct details),1,fpwrite);
fclose(fpwrite);
watu = 21;
break;
case 2:
printf("\nTry to read %s\n", datname);
fpread = fopen(datname, "r");
if (fpread == NULL)
{
printf("\nError open file\n");
watu = 21;
break;
}
while (fread(&b, sizeof(struct details),1, fpread))
{
printf("Plate No.... : %s\n", b.plateNo);
printf("Vehicle Type : %s\n", b.vehicleType);
printf("Car Brand... : %s\n", b.carBrand);
printf("Date ....... : %d\n ", b.date);
printf("Driver ID... : %d\n", b.driverID);
printf("Ticket Issued: %s\n", b.ticketNo);
}
fclose(fpread);
watu = 21;
break;
}while(watu != 0);
printf("\nEnd of Program\n");
return 0;
}
the row
Code:
while(fread(&bc[i++], sizeof(struct buch3), 1, infile))
writes 1 time a set of details.
When you have a fixed number like 10, it could be happen that
the Program writes any nonsens as data and you will have an error
when you will try to read the file.