The function fwrite() is for writing, the function fread() can be used for reading. Calculate the size of your struct customer, assume this size is N bytes. Then read in N bytes into a buffer of N bytes, for example a char array of N elements. Finally use pointer conversion or copy the bytes into the appropriate structure members.
Pointer conversion:
Code:
char binary_data [N];
struct customer *customer_data;
// read from file
fread (binary_data, sizeof (char), N, cfPtr);
// map data onto structure
customer_data = (struct customer *) data;
About the fclose():
Code:
if((cfPtr=fopen("cust_file.txt","r+"))==NULL)
{
printf("File could not be opened.\n");
}
else
{
printf("\n\nEnter account number. 1 to 100\n\n?");
scanf("%d",&new_cust.acct_num);
printf("\n\nEnter Customer Name\n\n?");
fscanf(stdin,"%s",new_cust.cust_name);
fseek(cfPtr,(new_cust.acct_num-1)*sizeof(struct customer),
SEEK_SET);
fwrite(&new_cust, sizeof(struct customer),1,cfPtr);
// You are now done with the file, so close it here.
fclose (cfPtr);
system ("cls");
}
// Not that if you get here, fopen() may have failed. If fopen()
// returns NULL, then after the if-branch, you get here and try
// to close a not opened file.
fclose(cfPtr);