Hi, writing data in a structure to file. That seems to be going fine. When I read it back though the last record is getting duplicated, so I get a double output of it to screen an it also doubles in calculations. Any suggestions? Below are the two functions I am using.
Code:
void add_customer()
{
struct customer new_cust={0,"",0,0};
logo();
if((cfPtr=fopen("cust_file.txt","r+"))==NULL)
{
printf("File could not be opened.\n");
}
else
{
printf("\n\nNew Customer Account Number:\t%d\n\n", cust_counter);
Sleep(1500);
fseek(cfPtr,(cust_counter-1)*sizeof(struct customer),SEEK_SET);
fread(&new_cust, sizeof(struct customer),1,cfPtr);
if(new_cust.acct_num!=0) //Validation to avoid account overwrite
{
printf("\nAccount %d already contains information.\n",new_cust.acct_num);
stall();
}
else
{
logo();
printf("\n\nEnter Customer Name\n\n?");
fscanf(stdin,"%s",new_cust.cust_name);
new_cust.acct_num=cust_counter;
fseek(cfPtr,(new_cust.acct_num-1)*sizeof(struct customer), SEEK_SET);
fwrite(&new_cust, sizeof(struct customer),1,cfPtr);
fclose(cfPtr);
cls();
}
}
}
void disp_cust_record()
{
if((cfPtr=fopen("cust_file.txt","r"))==NULL)
{
printf("File could not be opened.\n");
}
else
{
logo(); //Prints a logo
printf("\n\n%-6s%-16s%-11s%15s\n", "Acct", "Customer Name", "B_Code Start", "B_Code End");
while(!feof(cfPtr))
{
fread(&new_cust,sizeof(struct customer),1,cfPtr);
//////////////////////////////////////////>>>>>>>>>>>><<<<<<<<<<<<
if(new_cust.acct_num!=0)
{
printf("%-6d%-16s%-10d%10d\n", new_cust.acct_num,
new_cust.cust_name, new_cust.barcode_start, new_cust.barcode_end);
}
}
fclose(cfPtr);
stall(); //Generates a pause
}
}
I'd really appreciate help here, new to File I/O and battling this all week.
Thanks,
H
Code tags added by Hammer.