PriceFile* initialize_price_file(char *price_file_name, int file_mode) { PriceFile *price_file; FILE *dos_file_stream; ItemRecord **temp_cache_ptr; ItemRecord *temp_cache_data; unsigned i; ItemRecord dummy_record; unsigned *temp_record_number_ptr; price_file = (PriceFile *) malloc(sizeof(PriceFile)); dos_file_stream = fopen(price_file_name,"r+b"); if (dos_file_stream == NULL) dos_file_stream = fopen(price_file_name,"w+b"); if (dos_file_stream == NULL) { printf("Can't open price file %s.\n",price_file_name); exit(1); } price_file->dos_file_stream = dos_file_stream; temp_cache_ptr = (ItemRecord **)calloc(CACHE_SIZE, sizeof(ItemRecord *)); temp_cache_data = (ItemRecord *)calloc(CACHE_SIZE, sizeof(ItemRecord)); memset(&dummy_record,0,sizeof(ItemRecord)); dummy_record.upc[0] = (unsigned char)INVALID_BCD_VALUE; for (i = 0; i < CACHE_SIZE; i ++) { temp_cache_ptr[i] = temp_cache_data ++; *(temp_cache_ptr[i]) = dummy_record; } price_file->item_cache = temp_cache_ptr; temp_record_number_ptr = (unsigned *)calloc(CACHE_SIZE, sizeof(unsigned)); for (i = 0; i < CACHE_SIZE; i ++) temp_record_number_ptr[i] = INVALID_RECORD_NUMBER; price_file->file_record_number = temp_record_number_ptr; if (file_mode == CLEAR_FILE) { for (i = 0; i < FILE_SIZE; i ++) { position_record(price_file,i); fwrite(&dummy_record,1,sizeof(ItemRecord),dos_file_stream); } } return(price_file); }