Hi guys,
I need some help with a project...apart of it requires me to add, delete, edit and view records from a file, I have gotten the add to work so far but it will not delete records from the file, I am wondering if it has to do with me using append (the reason I use append is because I have to allow the user to add new records without deleting the old ones.
Here are the code fragments for add and delete:
add:
void add_customer(FILE *cfPtr)
{
int num_service;//variable to accept number of services
//creates blank customer record
CUSTOMER customer = {"", "", "", "", "", "", "", "", {""}, 0, 0.0};
//opens file, exits if it does not exist
if ((cfPtr = fopen("ajcustomer.dat", "ab")) == NULL)
{
printf("Customer File could not be opened.\n");
rewind(cfPtr);//sets the pointer to the beginning of the file
}
else{
textbackground(LIGHTCYAN);//changes the background colour to light cyan
clrscr();//clears the screen
textcolor(WHITE);//changes the text colour to white
cprintf(" CUSTOMER INFORMATION");
textcolor(LIGHTRED);//changes the text colour to red
cprintf(" [NEW!]");
printf("\n\n");
textcolor(WHITE);
cprintf(" Date of Birth Format : dd/mm/yyyy");
printf("\n\n");
cprintf(" Customer-ID : ");
scanf("%d", &customer.c_id);//scans customer's id into file
fflush(stdin);
//checks to make sure the customer ID does not pass 100 or is not less than or equal to 0
if(customer.c_id <= 0 || customer.c_id > 100)
{
cprintf(" Error, please enter a valid ID: ");
scanf("%d", &customer.c_id);//scans the new four customer id to file
}
fflush(stdin);//clears the buffer
printf("\n");//prints in a new line
cprintf(" First Name : ");
scanf("%s", &customer.fname[12]);//scans customer's first name into file
fflush(stdin);
printf("\n");
cprintf(" Last Name : ");
scanf("%s", &customer.lname[12]);//scans customer's last name into file
fflush(stdin);
printf("\n");
cprintf(" Date of Birth : ");
scanf("%s", &customer.dob[10]);//scans customer's last name into file
printf("\n");
fflush(stdin);
cprintf(" Phone 1 : ");
scanf("%s", &customer.land_line[8]);//scans customer's home phone number into file
fflush(stdin);
printf("\n");
cprintf(" Phone 2 : ");
scanf("%s", &customer.cell_phone[8]);//scans customer's cellular phone number into file
fflush(stdin);
printf("\n");
cprintf(" Address 1 : ");
scanf("%s", &customer.address_line1[50]);//scans customer's street address into file
fflush(stdin);
printf("\n");
cprintf(" Address 2 : ");
scanf("%s", &customer.address_line2[15]);//scans customer's city into file
fflush(stdin);
printf("\n");
cprintf(" Email : ");
scanf("%s", &customer.email[25]);//scans customer's emaail into file
fflush(stdin);
printf("\n");
cprintf(" How many services did the customer solicit? ");
scanf("%d", &num_service);//scans how many services the customer wishes to solicit
switch(num_service){
case 1: textcolor(WHITE);
printf("\n");
cprintf(" Service : ");
scanf("%s", &customer.serv_type.service1);//scans service the customer solicitted
fflush(stdin);
break;
case 2: textcolor(WHITE);
printf("\n");
cprintf(" Service [1] : ");
scanf("%s", &customer.serv_type.service1);
cprintf(" Service [2] : ");
scanf("%s", &customer.serv_type.service2);
fflush(stdin);
break;
case 3: textcolor(WHITE);
printf("\n");
cprintf(" Service [1] : ");
scanf("%s", &customer.serv_type.service1);
cprintf(" Service [2] : ");
scanf("%s", &customer.serv_type.service2);
cprintf(" Service [3] : ");
scanf("%s", &customer.serv_type.service3);;
fflush(stdin);
break;
case 4: textcolor(WHITE);
printf("\n");
cprintf(" Service [1] : ");
scanf("%s", &customer.serv_type.service1);
cprintf(" Service [2] : ");
scanf("%s", &customer.serv_type.service2);
cprintf(" Service [3] : ");
scanf("%s", &customer.serv_type.service3);
cprintf(" Service [4] : ");
scanf("%s", &customer.serv_type.service4);
fflush(stdin);
break;
case 5: textcolor(WHITE);
printf("\n");
cprintf(" Service [1] : ");
scanf("%s", &customer.serv_type.service1);
cprintf(" Service [2] : ");
scanf("%s", &customer.serv_type.service2);
cprintf(" Service [3] : ");
scanf("%s", &customer.serv_type.service3);
cprintf(" Service [4] : ");
scanf("%s", &customer.serv_type.service4);
cprintf(" Service [5] : ");
scanf("%s", &customer.serv_type.service5);
fflush(stdin);
break;
default:textcolor(WHITE);
printf("\n");
cprintf(" Enter a valid option!");
break;
}
printf("\n");
textcolor(WHITE);
cprintf(" Total Cost of service(s): ");
scanf("%f", &customer.total);
printf("\n");
fseek(cfPtr, (customer.c_id - 1) * sizeof(CUSTOMER), SEEK_SET);
fwrite(&customer, sizeof(CUSTOMER), 1, cfPtr);
cprintf(" File sucessfully created!");
printf("\n\n");
cprintf(" Press any key to continue...");
getch();
fclose(cfPtr);
}
menu();
}
delete:
void delete_customer(FILE *cfPtr)
{
CUSTOMER customer;
CUSTOMER blankCustomer = {"", "", "", "", "", "", "", "", {""}, 0, 0.0};
int cust_id;
if((cfPtr = fopen("ajcustomer.dat", "rb+"))== NULL)
{
textbackground(LIGHTCYAN);
textcolor(WHITE);
cprintf("The Customer File could not be opened.");
printf("\n");
}
else {
textbackground(LIGHTCYAN);//changes the background colour to light cyan
clrscr();
textcolor(WHITE);//changes text colour to white
cprintf(" CUSTOMER INFORMATION");
textcolor(LIGHTRED);//changes text colour to red
cprintf(" [DELETE!]");
printf("\n\n");
textcolor(WHITE);
cprintf(" Enter the Customer-ID of the Customer you wish to delete.");
printf("\n\n");
cprintf(" Customer-ID : ");
scanf("%s", &cust_id);
fflush(stdin);
fseek(cfPtr, (cust_id - 1) * sizeof(CUSTOMER), SEEK_SET);
fread(&customer, sizeof(CUSTOMER), 1, cfPtr);
if (customer.c_id == 0)
{
textbackground(LIGHTCYAN);//changes the background colour to light cyan
textcolor(WHITE);
cprintf("\n That Customer account does not exist.\n");
}
else
{
fseek(cfPtr, (cust_id - 1) * sizeof(CUSTOMER), SEEK_SET);
fwrite(&blankCustomer, sizeof(CUSTOMER), 1, cfPtr);
textbackground(LIGHTCYAN);//changes the background colour to light cyan
textcolor(WHITE);//changes text colour to white
cprintf("\n\n The Customer account has been successfully deleted.", customer.c_id);
printf("\n\n");
cprintf(" Press any key to continue...");
getch();
}
fclose(cfPtr);
}
menu();
}
The project is due tommorow and I have been trying fruitlessly to make it work, I've tried changing write and read mode for the delete but it doesn't work...If anyone can help I'd appreciate any suggestions
Thanks in advance