#include #include #include //This program will create the file that is used in LAB 7 int main() { int ID, i, j; char name[20], data_read[20]; float balance; char c; struct Record { int i; char c; char a[20]; float b; } P; printf("Size of struct = %lu\n",sizeof(P)); FILE *fp; fp=fopen("Testcase.bin", "w"); printf(" Enter 1 to continue, 0 to quit "); scanf("%d", &i); fflush(stdin); while (i != 0) { printf(" ID (Integer) : "); fgets(data_read, 18, stdin); //fflush(stdin); sscanf(data_read, "%d", &ID); printf("Encoded? E or n :"); fgets(data_read, 18, stdin); //fflush(stdin); sscanf(data_read, "%c", &c); printf("Balance "); fgets(data_read, 18, stdin); //fflush(stdin); sscanf(data_read, "%f", &balance); printf("name :"); fgets(data_read, 18, stdin); //fflush(stdin); sscanf(data_read, "%d", &ID); data_read[strlen(data_read)]='\0'; P.i=ID; P.c=c; P.b=balance; strcpy(P.a, data_read); if(c == 'E') {for(i=0;i<20;i++) P.a[i]=P.a[i]^0xFF;} //Copy the record to the file fwrite(&P, sizeof(P), 1, fp); /************************************* **************** IGNORE THIS PART ******* I was trying to know the difference between writing the record and the fields separetly fwrite(&(P.i), sizeof(P.i), 1, fp); fwrite(&(P.c), sizeof(P.c), 1, fp); fwrite((P.a), sizeof(P.a), 1, fp); fwrite(&(P.b), sizeof(P.b), 1, fp); **************************************/ printf(" Enter 1 to continue, 0 to quit "); scanf("%d", &i); fflush(stdin); } fclose(fp); }