#include #include struct apair /* defione a structure with two elements*/ { int color_count; int color_num; }; void main (void) { FILE *image_file; struct apair image_pair; int i; clrscr(); /* open */ if ((image_file =fopen("c:\itest.abc", "wb")) == NULL) { printf("Cannot open c:\itest.abc file, quitting.\n Press any key."); getch(); exit(1); } else { printf("\n file created ok. Press any key to continue"); getch(); } clrscr(); for (i=1;i<=20;i++) { image_pair.color_count=i; image_pair.color_num=2*i; fwrite(&image_pair, sizeof(image_pair),1,image_file); } if(fclose(image_file)==0) { printf("\n 20 pairs written to file ok. Press any key to continue"); getch(); } else { printf("\n errors on closing, terminating"); exit(1); } if ((image_file =fopen("c:\itest.abc", "rb")) == NULL) { printf("Cannot reopen file"); exit(1); } else { printf("\n file reopened for reading, ok. Press any key to continue"); getch(); } clrscr(); for (i=1;i<=20;i++) { image_pair.color_count=i; image_pair.color_num=2*i; fread(&image_pair, sizeof(image_pair),1,image_file); printf("\n color %d count %d",image_pair.color_count,image_pair.color_num); } if(fclose(image_file)==0) { printf("\n closed ok. Press any key to continue"); getch(); } else { printf("\errors on close, terminating"); exit(1); } closegraph(); exit(0); }