can anyone tell me why the following code crashes my computer on reaching the end of the file ?????
its just reading from a binary file with the data structure as shown. it prints -
etc
(null) EGGSHELL CREAM 02 006529 0350 900805 007.99
(null) EGGSHELL SUNRISE 03 006789 1250 900618 008.00
(null) EGGSHELL LILAC 02 006015 0400 900115 007.50
(null) EGGSHELL PINK 00 001250 1000 900725 007.00
(null) EGGSHELL OFF WHITE 02 008025 0500 900623 007.99
(null) EGGSHELL PALE GOLD 00 001023 0150 900426 007.00
(null) EGGSHELL SUPERWHITE 04 008957 1500 900418 007.99
(null) EGGSHELL MAGENTA 00 002394 0500 900428 007.89
(null) EGGSHELL LEMON 00 007500 0500 900621 007.00
end of file
then crashes.
all the data is ok except the first field , why is this printed as null ???
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <isam.h>
#include <common.h>
FILE *fp_stock_master;
FILE *fp_out;
RECORD_DATA *stock_rec;
int main()
{
if((fp_stock_master=fopen("A:STCKMAST.DAT","rb"))= =NULL)
{
printf("ERROR OPENING STOCK MASTER FILE\n");
exit(1);
}
else
printf("OPENING STOCK MASTER FILE\n");
do
{
if(fread(stock_rec,sizeof(RECORD_DATA),1,fp_stock_ master)!=1)
{
if(feof(fp_stock_master))
{ printf("end of file");}
else if(!feof(fp_stock_master)) /*else reading error*/
{printf("\nERROR READING CUSTOMER MASTER FILE - PROGRAM TERMINATED");exit(1); }
break;
}
if(!feof(fp_stock_master) )
printf("%6s %19s %02d %06ld %04d %06s %06.2f\n",stock_rec->key,stock_rec->desc,
stock_rec->supp_code,stock_rec->free_stock,stock_rec->min_stock,
stock_rec->movement_date,stock_rec->price);
}
while(!feof(fp_stock_master));
fclose(fp_stock_master);
return 1;
}
/*
typedef struct
{
char key[7];
char desc[20];
short supp_code;
long free_stock;
short min_stock;
char movement_date[7];
float price;
} RECORD_DATA;
this struct is in a header file */