I've written this program, accessing a binary file and then comparing the data and writing to a new file, however the program on msdos does not work. I can't find the errors. Please Help.
/*************************
**********
**********
**********
**********
*********/
/*This program is written for Zenith Paints Co. to provide a solution */
/*The new sorted file from program two is used to update a line sequential*/
/*master file and this program will create a new master file. */
/*reference is made to an indexed sequential stock master file for price */
/*information. A header is used to access this information called 'isam.h'*/
/*Any invalid transactions are then printed onto an error report. */
/*************************
**********
**********
**********
**********
*********/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#include "a:isam.h" /*a header file storing stock data*/
#include"a:printer.h"
#define SIZE sizeof(struct master)
#define ISSUE struct I_R
#define DELREC struct deletion
#define CREREC struct create
#define SIZE_U sizeof(union valid)
#define PRINT "LPT1"
#define TRUE 1
#define FALSE 0
#define LASTCODE "ZZZZZ" /*ensures that if one file finishes first
the other one will continue to be processed*/
/*declare structures and union to hold the customer details*/
/*These structures are determined by the file specifications*/
struct stock /*structure held on header file*/
{
char cust_code[6],
cust_name[21],
cust_add[61];
float cust_bal;
long credit_limit;
int year,
month,
day;
} ;
struct stock new_s;
struct master /*structure held on master file*/
{
char cust_code[6],
cust_name[21],
cust_add[61];
float cust_bal;
long credit_limit;
int year,
month,
day;
};
struct I_R /*The structure for issue/receipt record*/
{
char rec_type,
cust_code[6],
part_num[7],
iss_rec_qty[5];
};
struct deletion /*The structure for delete record*/
{
char rec_type,
cust_code[6];
};
struct create /*The structure for create record*/
{
char rec_type,
cust_code[6],
cust_name[21],
cust_add[61],
cust_bal[10],
cust_limit[8];
};
union valid /*Union to hold all three structures*/
{
ISSUE i;
DELREC d;
CREREC c;
};
/*************************
**********
**********
**********
**********
****/
/*Global declarations. */
/*************************
**********
**********
**********
**********
****/
struct tm *todays_date;
time_t calendar_time;
/*declare function prototypes and theire names indicate*/
/*their purpose*/
void store_new(FILE *fp, struct master temp);
void print_headings(FILE *out_prn);
void error(FILE *out_prn, union valid u, int i);
/*************************
**********
**********
**********
**********
****/
/* coding for solution */
/*************************
**********
**********
**********
**********
****/
void main()
{
FILE *cp, *sort, *out_prn, *new_fp; /*file pointers for the four file*/
struct master temp; /*structure variable*/
union valid u; /*union to hold the structures */
int compare, records=0; /*compare variable to hold comaprison of*/
/*two structure*/
char ch;
char cust_code[4]; /*to hole the customer code*/
float total=0;
RECORD_DATA *stock_rec;
calendar_time = time(NULL);
todays_date = localtime(&calendar_time);
/*to access the header file stock details*/
if((out_prn=fopen(PRINT,"w
t"))==NULL
)
{
fprintf(stderr,"Error cannot open\n");/*if file does not open then exit*/
exit(1);
}
if((cp=fopen("A:Custmast.d
at","rb"))
==NULL)
{
fprintf(out_prn,"Error cannot open\n");/*if file does not open then exit*/
exit(1);
}
if((sort=fopen("a:sort.dat
","rb"))==
NULL)
{
fprintf(out_prn,"Error cannot open sort\n");/*if file does not open then exit*/
exit(1);
}
if((new_fp=fopen("A:new.da
t","wb+"))
==NULL)
{
fprintf(stderr, "Error cannot open\n");/*if file does not open then exit*/
exit(1);
}
print_headings(out_prn); /*declare fundion to print headings*/
if(fread(&u,SIZE_U, 1, sort)!=1)
{
fprintf(stderr, "Unable to read sort file\n");/*if file does not open then exit*/
exit(1);
}
/*read first record from sorted file*/
if(fread(&temp, SIZE, 1, cp)!=1) /*read first record from master file*/
{
fprintf(stderr, "unable to read master file\n");/*if file does not open then exit*/
exit(1);
}
/*copy 'ZZZZZ' to customer code to compare*/
/*if last record*/
strcpy(u.i.cust_code, LASTCODE);
while(strcmp(u.i.cust_code
, LASTCODE)!=0 ||
strcmp(temp.cust_code, LASTCODE)!=0)
{
/*store value in compare*/
compare=strcmp(u.i.cust_co
de, temp.cust_code);
/*there are a few possibilities here as */
/*if the answer is yes than a check is */
/*done for the record type before a new */
/*record is read*/
if(compare<0)
{
ch=u.i.rec_type;
ch=toupper(ch);
if(ch=='C')
store_new(new_fp, temp);
else
if(ch=='I'||ch=='R'||ch=='
D')
error(out_prn,u, 1);
if(fread(u.i.cust_code, SIZE_U, 1, sort)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
}/*end if*/
if(compare>0)
{
store_new(new_fp, temp);
if(fread(&temp, SIZE_U, 1, cp)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
} /*end if*/
if(compare==0)
{
ch=u.i.rec_type;
ch=toupper(ch);
switch(ch)
{
case 'I':
case 'R':
if((stock_rec=search_file(
cust_code)
)!=NULL)
{
total=atoi(u.i.iss_rec_qty
) * stock_rec->price;
}
if(ch=='I')
temp.cust_bal+=total;
else
if(ch=='R')
temp.cust_bal-=total;
else
fprintf(out_prn,"Error in I\n");
if(fread(&u, SIZE_U, 1, sort)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
store_new(new_fp, temp);
break;
case 'D':
if(temp.cust_bal!=0)
fprintf(out_prn,"Error in D\n");
if(fread(&u, SIZE_U, 1, sort)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
else
//eof1=readfile(sort);
if(fread(&u, SIZE_U, 1, sort)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
if( fread(&temp, SIZE, 1, cp)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(temp.cust_code, LASTCODE);
} /*end if*/
break;
case 'C': error(out_prn,u, 2);
if(fread(&u, SIZE_U, 1, sort)!=1)
{
/*if eof occurs store lastcode in cust_code*/
strcpy(u.i.cust_code, LASTCODE);
} /*end if*/
break;
default: break;
}/*end switch*/
} /*end if*/
records++;
fflush(stdin);
}/*end while till eof*/
fclose(new_fp);
fclose(out_prn);
fclose(sort);
fclose(cp);
} /*end main*/
/*************************
**********
**********
**********
**********
****/
/*store_new() */
/*Parameter: new_fp. A pointer to the new file */
/* temp. structure in old master file */
/* */
/* Return: none. */
/* */
/* A function that will use live local time and date each time the */
/*old data is stored into the new structure and then written to the */
/*new file */
/*************************
**********
**********
**********
**********
****/
void store_new(FILE *new_fp, struct master temp)
{
int loop=0;
struct tm today;
time_t time_in_secs;
time_in_secs = time(NULL);
today = *localtime(&time_in_secs);
strcpy( new_s.cust_name,temp.cust_
name);
strcpy(new_s.cust_add,temp
.cust_add );
strcpy(new_s.cust_code, temp.cust_code );
new_s.cust_bal=temp.cust_b
al;
new_s.credit_limit=temp.cr
edit_limit
;
new_s.year=today.tm_year;
new_s.month=today.tm_mon+1
;
new_s.day=today.tm_mday;
if(fwrite(&new_s, sizeof(struct stock), 1, new_fp)!=1)
{
printf("Error in writing to new file");
exit(1);
} /*end if*/
loop++;
return;
} /*end function*/
/*************************
**********
**********
**********
**********
****/
/*error() */
/*Parameter: i. An int to indicate the message required. */
/* out_prn. pointer to the printer file */
/* u. Pointer to the union. */
/* Return: none. */
/* */
/* A simple routine to display an appropriate error message. */
/*************************
**********
**********
**********
**********
****/
void error(FILE *out_prn, union valid u, int i)
{
char *e[]={"Error\nError in creation\nError in Delete"};
fprintf(out_prn,"\t%s\t\t\
t%s\n", u.i.cust_code, e[i]);
} /*end function*/
/*************************
**********
**********
**********
**********
****/
/*print_headings() */
/*Parameter: out_prn. A pointer to the printer file */
/*Return: none. */
/* */
/* A function to display the errors for the report. */
/*************************
**********
**********
**********
**********
****/
void print_headings(FILE *out_prn)
{
static int page;
page++;
bold(out_prn,ON);
condensed(out_prn, OFF);
fprintf(out_prn,"\n\tTRANS
ACTIONS FOR ZENITH PAINTS CO%30.24sPage %3d\n\n"
"\t\t\t",asctime(todays_da
te), page);
fprintf(out_prn,"\tCust Code\t\t\tError Reason\n\n");
bold(out_prn,OFF);
}/*end func*/