Hey! this is the part of my code which uses this function. Basically, the first time the user, uses the program I want the strcut flag to be set to zero.
Code:
main()
{
pass customers[ROW][LABEL];
int i, j;
long empty;
clear_screen();
atexit(thank_you);
read_file(customers);
empty = file_check();
if(empty == 0) //If there is no data in the file loaded then we should initialise flag to zero for all structs.
{
for(i = 0; i < ROW; i++)
for(j = 0; j < LABEL; j++)
customers[i][j].flag = 0;
}
}
Code:
void read_file(pass cust[][LABEL])
{
FILE* fptr;
fptr = fopen(FNAME, "rb");
if(fptr == NULL)
{
fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
if(yesno("Do you wish to create this file?"))
{
write_file(cust);
fptr = fopen(FNAME, "rb");
}
else
exit(1);
}
fread(cust, sizeof(cust), 1, fptr);
fclose(fptr);
}
Code:
void write_file(pass cust[][LABEL])
{
FILE *fptr;
fptr = fopen(FNAME, "wb");
if(fptr == NULL)
{
fprintf(stderr, "Could not open %s - %s\n", FNAME, sys_errlist[errno]);
exit(1);
}
fwrite(cust, sizeof(cust), 1, fptr);
fclose(fptr);
}
Code:
long file_check()
{
FILE *fptr;
int size;
fptr = fopen(FNAME, "rb");
fseek(fptr, 0, SEEK_END);
size = ftell(fptr);
return size;
}
Thanks for your help! Its much appreciated!