/* a simple example of using fread and fwrite * to read and write an array of structures */ #include //#include //#include #include #define NELEM 3 int main(void) { FILE *fp; // the product structure struct product { int cat_num; float cost; }; typedef struct product product; product productarr[NELEM] = {{2,20.1},{4,40.1},{6,60.1}}; product one_product, *product_ptr = &one_product; int i, irc; // return code fp = fopen("c11-struct-file","w+"); assert(fp != NULL); // write the entire array into the file pointed to by fp irc = fwrite(productarr, sizeof(product), NELEM, fp); printf(" fwrite return code = %d\n", irc); // prpare for reading from the beginning of the file rewind(fp); // read from the file one product at a time for (i=0; icat_num, product_ptr->cost); } return 0; }