I am writing a program that saves all of its input to a binary file. I have three entries: your code(1-5), date, and sale amount. I have the program storing everything in the file, but I need to access information by the code that it was entered under. The first function retrieves the corresponding name for the code entered and them adds up and prints the corresponding sales. Now I seem to have it working fine, but the second function I need to have the user enter the code the program retrieve the date along with price for that date, and if there is more that one date it will print them all with sales. I can get it to print all the dates and sales that were entered, but not just the ones for the code entered. I am trying to use fseek() but I am not too sure how to use it or even if it is what I should be using. Any advise would be awesome!!
Here is the two functions that I mentioned above:
Code:
void total_sales( )
{
double totsales[6] = {0};
double avg, tot = 0;
sales rec;
int i;
FILE *file;
file = fopen("sales.dat", "rb");
if (file == NULL) { message("Error opening file"); return;}
printf("\nTotal Sales Report\n\n");
printf("\n\nRealtor\t\t\t\t\tTotal Sales ($)\n\n");
while (fread(&rec, sizeof(rec), 1, file))
totsales[rec.code] += rec.sale;
for (i = 1; i < 6; i++)
printf("%s\t\t%22.2lf\n", *(name + i), *(totsales + i));
fclose(file);
getaverage(totsales, &tot, &avg);
printf("\n\nAverage = %29.2lf ", avg);
printf("\nTotal = %31.2lf", tot);
printf("\n\nPress Enter to continue");
getchar( );
}
/*======================================================*/
void agent_sales( )
{
int i, cd, c, recnum, num, status;
double totsales[6] = {0};
long spot, last, count;
char ch;
sales rec;
FILE *file;
file = fopen("sales.dat", "rb");
if (file == NULL) { message("Error opening file"); return;}
cd = getcode( );
i = cd;
printf("\n\nSales by %s\n\n", *(name + cd));
printf("Date\t\t\t\t\t Sale\n");
//fseek(file, 0L, SEEK_END); // get current record count from file, assign to c
//c = (int)ftell(file) / sizeof(rec);
while (fread(&rec, sizeof(rec), 1, file))
{
totsales[rec.code] = rec.sale;
printf("%s\t\t%17.2lf\n", (rec.date ) , *(totsales + i));
}
fclose(file);
printf("\n\nPress Enter to continue");
getchar( );
}