Hello I am having a problem, I need my program to write data to a .dat file, then I need it to report/show my data, however I keep getting an error 'Access violation reading location', when I try to get it to show/report the data written? When I did some troubleshotting I noticed that gibberish is being written to my .dat file, is that why I am getting the error?
Here is my code adding the data
Code:
void add()
{
int inventory, catagory, quantity, choice, total_products = 0, ctyp, c, items, key;
double cost, price, cateType[6], costs, category;
int n = 0, count = 0;
char discript[80];
int MININ, MAXIN, MINCAT, MAXCAT, MINQT, MAXQT;
double MINCT, MAXCT, MINPR, MAXPR;
SGDATA rec;
FILE *fp, *sp;
fp = fopen("limits.dat", "rt");
if (fp == NULL)
{
printf("Error opening file\n");
perror("Error opening file");
getchar();
}
else
{
while (fscanf(fp, "%i%i%i%i%i%i%lf%lf%lf%lf", &MININ, &MAXIN, &MINCAT, &MAXCAT, &MINQT, &MAXQT, &MINCT, &MAXCT, &MINPR, &MAXPR) != EOF)
{
fclose(fp);
}
sp = fopen("limits1.dat", "ab");
if(sp == NULL)
{
printf("Error opening file");
return;
}
fseek(sp, 0L, SEEK_END);
c = (int)ftell(sp) / sizeof(rec);
init_costs(cateType, 6);
do
{
rec.inventory = validi(MININ, MAXIN, "Product number");
rec.catagory = validi(MINCAT, MAXCAT, "Product Type");
getstring(rec.discript);
strcase(rec.discript);
rec.quantity = validi(MINQT, MAXQT, "Quantity");
rec.cost = validf(MINCT, MAXCT, "Cost");
rec.price = validf(MINPR, MAXPR, "Price");
show(rec, rec.discript);
key = prompt("Are entries accurate?");
if(key == 'Y')
{
items = fwrite(&rec, sizeof(rec), 1, sp);
if(items == (int)NULL)message
("Error writing to file");
else c++;
}
fclose(sp);
printf("\nEnter category type (1-5);\n 1- Camping\n 2- Tennis\n 3- Golf\n 4- Snow sports\n 5- Water Sports ");
scanf("%d%*c", &ctyp);
printf("\nEnter Cost of Product Type: ");
scanf("%lf%*c", &costs);
cateType[rec.catagory] += costs*rec.quantity;
show(rec, rec.discript);
choice = prompt("Continue");
}
while (choice == 'y' || choice == 'Y');
show_costs(cateType, 6);
getchar();
}
}
Here is my code to show/report my data
Code:
void report()
{
int c, num = 0;
double total_profit;
SGDATA rec;
FILE *sp;
sp = fopen("limits1.dat", "rb");
if(sp == NULL)
{
printf("Error opening file\a");
return;
}
titles();
c = headings();
while(fread(&rec, sizeof(rec), 1, sp))
{
++num;
printf("-4i%8i%8s%8i%8lf%12lf%12lf\n", rec.inventory, rec.catagory, rec.discript, rec.quantity, rec.cost, rec.price,
profit(rec.price, rec.cost, rec.quantity));
//total_profit += profit();
//printf("Total expected profit %5.1f", total_profit);
}
line('=', c-1);
printf("Done");
getchar();
fclose(sp);
}
I really appreciate any help.... SDI