Hi,
I'm trying to debug a C program that I have written and getting nowhere fast.
It involves reading from a file, validating the data and writing this back to another file.
To write it back I'm using a union of 3 structures( as it can be one of 3 types of record). However, the program runs ofr around 20 records and then crashes with a windows, you have accessed memory error. So I've looked for an array that isn't big enough etc but everything looks ok.
Wondered if it might have something to do with the union, as I initialise the longest structure in it on each pass, so as not to carry over any data to the next record.
Here's the main function of the program.
Thanks
Andy.
void quantity(void);
void error(int i);
void valid(void);
void valid_part_number(void);
char Pause(void);
void read(void);
void valid_cust_name(void);
void valid_address(void);
void cust_balance(void);
void cust_credit(void);
FILE *fp_ptr,
*prn_ptr,
*valid_ptr;
struct ir_rec { /* Insert/replace record */
char type, /* record type */
cust_code[5], /* customer code */
part_no[6], /* part number */
quantity[20]; /* issue/ received quantity */
};
struct del_rec { /* deletion record */
char type,
cust_code[5];
};
struct create_rec { /* creation record */
char type,
cust_code[5],
cust_name[20], /* cust name */
cust_address[60], /* address */
cust_balance[9], /* balance */
cust_credit_limit[7]; /* credit limit */
}dummy;
union records {
struct ir_rec ir;
struct del_rec del;
struct create_rec create;
};
union records transaction; /* transaction is union name */
char big[150],
*trans_ptr,
record[5];
int *big_ptr,
delta = 0,
lines = 0;
void main()
{
char ch;
struct create_rec dummy = {'\0',
"\0",
"\0",
"\0",
"\0",
"\0" };
/* open file for input */
if(( fp_ptr = fopen("A\:TEST.txt","r")) == NULL)
{
printf("\n Error in opening input file\n");
exit(1);
/* can't open or find file, exit to OS */
}
/* open printer file */
/* if(( prn_ptr = fopen("A:\PRN1.dat","wb")) == NULL)
{
printf("\nError opening printer for output\n");
Pause();
exit(1); */
/* printer unavailable, exit to OS */
/* open file for output */
if(( valid_ptr = fopen("A:\VALIDATED.dat","wb+")) == NULL)
{
printf("\nError creating output file\n");
exit(1);
/* unable to create/write to output file */
}
/* print headings */
fprintf(prn_ptr, "FILE ERROR REPORT\n");
lines = 2;
/* read in from file */
printf("file read");
Pause();
while(!feof(fp_ptr))
{
transaction.create = dummy;
fscanf(fp_ptr, "%[^\n]", big);
ch = fgetc(fp_ptr); /* increments file pointer to skip newline character */
/* printf("\n %s", big);
Pause(); */
ch = toupper(*big);
printf("\n%c first chararcter", ch);
/* if(ch ==' ')
{
error(5);
} */
switch(ch)
{
case 'I': valid();
valid_part_number();
quantity();
break;
case 'R': valid();
valid_part_number();
quantity();
break;
case 'D': valid_part_number();
break;
case 'C': valid_cust_name();
valid_address();
cust_balance();
cust_credit();
break;
default: error(3);
}
/* write validated record to file */
fwrite(&transaction, sizeof(transaction.create), 1, valid_ptr);
}
fcloseall();