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

Recommended Answers

All 24 Replies

theres no way i can help very much with your code.

one problem here is i dont have enough information to try and test your code.

but, I'll go ahead and try and explain how you should approach this problem...


EXECUTIVE SUMMARY: All of this totally depends on how your text file is organized if your text file is organized logically and in clearly fixed patterns, it will make your job a lot easier.

IF each record in your file is a fixed size (meaning each record has the same number of fields, and each field is the same width), THEN you can relatively painlessly use fseek() and/or rewind() to modify text within a file.

BUT if your records and/or fields vary in number and size, then you're going to have a helluva time.

"The Easy Option" -- which may not be one that your instructor will accept -- is to read the entire contents of your data file into memory organized into arrays and/or structures, then modify the memory contents, then rewrite the contents into a new file (ie, overwriting the original)

is that an option for you? i dont particularly like it, because it's not a very scalable solution. but it might be an option to turn in a program at the last minute that "works"

otherwise, you'll have to do it like this:

to add a record, merely open a file to append and the additional info will be appended after the last record. fairly easy, but pay attention to the whitespace, that you don't accidentally throw in additional newlines.

to modify a record, selectively search for each field that is being replaced and overwrite it using fseek() and/or rewind() along with fwrite() ... or possibly you can replace an entire group of fields as one entire record.

to delete a record, will be trickier. you can overwrite each field with blank spaces, but then you're left with an entire block of blank spaces (holes) within your file. this can screw up an ability to properly search (modify) files beyond the blank spaces if you're not careful.

one option could be to insert new records into blank spaces left by deleted records, if there are any. this will be somewhat complex.

another option is to re-read all information past the deleted space into memory, delete everything to just prior to the deleted record, then rewrite all the other records after that point... thereby "closing" the "hole" -- but this is practically the same as the "Easy Option" i suggested above. if you're going to spend the resources to do this you might as well go all the way.

good luck.

if you include an example of your text file, i might be able to help more.


.

Hey jephthah,
thanx so much for your help, I've attached all of the coding I've done so far maybe that might make it a little less ambiguous.

this is going to be difficult for me, becuase your code is full platform-specific terminal colors, and calls to a "cprintf()" function that is deprecated even in MSFT's crapyy <conio.h> library. I'm going to have to remove all of that stuff just to get the code to compile.

you really should consider getting rid of all this obscure coding practice that is specific to old microsoft platforms. your code just wont work on a probably 50% of the machines in use today.

another major problem is your widespread use of fflush(STDIN). that is a huge mistake never do that. get rid of those statements, and cuss out whoever told you that is acceptable program development practice.

this is going to be difficult for me, becuase your code is full platform-specific terminal colors, and calls to a "cprintf()" function that is deprecated even in MSFT's crapyy <conio.h> library. I'm going to have to remove all of that stuff just to get the code to compile.

you really should consider getting rid of all this obscure coding practice that is specific to old microsoft platforms. your code just wont work on a probably 50% of the machines in use today.

another major problem is your widespread use of fflush(STDIN). that is a huge mistake never do that. get rid of those statements, and cuss out whoever told you that is acceptable program development practice.

sorry about all the platform specific things its just my instructor just needs the code to run in borland so I want to make the interface as pleasing as possible..the fflush now I used that because it keeps skipping through scans and that the only thing I could think of to use to get rid of it... but I mean maybe you dont really have to run it..let me outline how it works it basically creates a random access file and allows me to add records to it, each record will have an id, first name, last name, dob, land_line, cell_phone, address_line1, address_line2, email, service_type, total...so that wat each record in the file will consist of....the add works perfectly when i use append but the delete will not work

your terminal graphics, from what i can tell, are VERY nice.

unfortunately, i cant get any of it to compile.

one question: where are you that your instructor requires Borland?

LOL the very backward country Jamaica....we use visual in c++ but for C programming all the machines at school use borland and so when we are gonna run our code it has to gone in borland

oh dear. i really do think Borland is exploiting the schools in countries like Jamaica and India.

no one of any credibility uses Borland here in the States, that I'm aware of. Borland used to be a leading software company in the 80s and early 90s, but now they've turned into essentially a really bad joke. it's quite sad.

And I'm no microsoft fan eitehr, to be sure. any of the GCC deriviatives are based on industry standards, and are completely FREE.

but i digress.

i apologize for getting your hopes up, but theres no way i can get your code to work over here at this point. i have to rip everything out, and rewrite half of it.

some days i think about putting borland on just so i can try and help some of you folks out there, but it's a huge PITA, and i dont want to screw up my work environment here at my job. and i dont even use windows at home, so theres just nothing much i can do.

maybe someone else has borland?

otherwise, read my suggestions and see what you can do.

did you consider my "easy solution" to rewrite the file after each modification? is that even an option for you or are you required to modify the text on-the-fly?

no really what the instructor wants is that each time you enter the program you can add to the file without overwriting what was there before so that why I used append but then the delete record will not work for me to delete a specific record in the file....thanx though for trying I appreciate it

jepthah is right, there's far too much proprietary code for anyone here to help you. Not to mention fflush(stdin), and fixing your program so you don't require that could take quite some time. I will point out a fairly simple thing you can do to improve your program however, change this:

scanf("%s", &customer.fname[12]);

To:

scanf("%s", customer.fname);

You seem to think that [12] passes all the empty characters to scanf to fill, but actually you're just passing one character (the thirteenth character). Change all your string scanfs in the same way because they're invoking undefined behavior right now.

ohh wow thanx I didn't even see that thanx much

I've got to hand it to you, your styling is very nice, and obvious you spent a lot to make it look sharp with the colors and terminal graphics..

but to be honest, you really ought to consider the time it takes to do all of that, when you have problems getting your code to work.

basically: get it to work first. THEN add the eye candy.

for one thing, its damn near impossible for anyone to help you with all the extra stuff confusing the matter.

so now here is your code, with all the Borland crap stripped out. maybe we can work on figuring out what the problem is....

#include <stdio.h>//standard input/ouput header

void menu(void);//Function Prototype for user menu
void loading(void);//Function Prototype for loading function
void add_customer(FILE *cfPtr);//Function Prototype to add new customer record(s)                                                
void delete_customer(FILE *cfPtr);//Function Prototype to delete customer record(s)
void edit_customer(FILE *cfPtr);//Function Prototype to edit customer record(s)
void view_customer(FILE *cfPtr);//Function Prototype to view customer record(s)
void add_service(FILE *crptr);//Function Prototype to add ner service record(s)
void delete_service(FILE *crptr);//Function Prototype to delete service record(s)
void edit_service(FILE *crptr);//Function Prototype to edit service record(s)
void view_service(FILE *crptr);//Function Prototype to view service record(s)

union service_type
{
   char service1[10], service2[10], service3[10], service4[10];
   char service5[10];
};

struct customer
{
   char fname[12], lname[12], dob[10], land_line[8], cell_phone[8];
   char address_line1[50], address_line2[15], email[35];
   union service_type serv_type;
   int c_id;
   float total;
};

typedef struct customer CUSTOMER;//new alias for customer structure

struct service
{
   int s_id;
   char customer_name[35];
   char s_type[35];
   float cost;
};

typedef struct service SERVICE;//new alias for service structure

int main(void)
{
   menu();//call to menu function
   return 0;
}

void menu()
{
   int menu_option;//variable for storing user's option
   FILE *cPtr, *crPtr;//pointer to ajcustomer and ajservice files

   printf("                  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 201, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 187);
   printf("\n");
   printf("                  %c     ^^AUTO JAZZ CAR WASH DATABASE^^    %c", 186, 186);//prints text in white
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c             User Menu                  %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c         Customer Database              %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c [1] Add a New Customer to the Database %c", 186, 186);
   printf("\n");
   printf("                  %c [2] Delete a Customer File             %c", 186, 186);
   printf("\n");
   printf("                  %c [3] Edit a Customer File               %c", 186, 186);
   printf("\n");
   printf("                  %c [4] View a Customer's File             %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c         Services Database              %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c [5] Add a New Service Record           %c", 186, 186);
   printf("\n");
   printf("                  %c [6] Delete a Service Record            %c", 186, 186);
   printf("\n");
   printf("                  %c [7] Edit a Service Record              %c", 186, 186);
   printf("\n");
   printf("                  %c [8] View a Service Record              %c", 186, 186);
   printf("\n");
   printf("                  %c [0] Exit                               %c", 186, 186);
   printf("\n");
   printf("                  %c                                        %c", 186, 186);
   printf("\n");
   printf("                  %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 200, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 205, 188);
   printf("\n");
   printf("                              Enter an option: ");
   scanf("%d", &menu_option);//accepts user's option
   if(menu_option != 0)
   {

      switch(menu_option)
      {

         case 1:
                 loading();//call to loading function
                 add_customer(cPtr);//call to add_customer function
                 break;//exit switch

         case 2:
                 loading();//call to loading function

                 delete_customer(cPtr);//call to delete_customer function
                 break;//exit switch

         case 3:
                 loading();//call to loading function

                 printf("edit_customer");//call to edit_customer function
                 break;//exit switch

         case 4:
                 loading();//call to loading function

                 printf("view_customer");//call to view_customer function
                 getch();
                 break;//exit switch

         case 5:
                 loading();//call to loading function

                 printf("add_service");//call to add_service function
                 break;//exit switch

         case 6:
                 loading();//call to loading function

                 break;//exit switch

         case 7:
                 loading();//call to loading function

                 break;//exit switch

         case 8:
                 loading();//call to loading function

                 break;//exit switch

         default:
                 loading();//call to loading function

                 printf("Enter a valid option!");
                 break;//exit switch
      }
   }
   else
   {
      exit(0);
   }
}

void loading()
{
   int counter, count;//variables for looping loading bar

   printf("\n\n\n\n");
   printf("                                   _._\n");
   printf("                              _.-=%c%c-         _\n", 34, 95);
   printf("                         _.-=%c   %c-          | ||%c%c%c%c%c%c%c---._______     __..\n", 34, 95, 34, 34, 34, 34, 34, 34, 34);
   printf("             ___.===%c%c%c%c-.______-,,,,,,,,,,,,`-''----%c %c%c%c%c%c       %c%c%c%c%c  %c%c%c\n", 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 95, 95, 39);
   printf("      __.--%c%c     __        ,%c     AUTOJAZZ      o %c           __        [__|\n", 34, 34, 39, 92);
   printf("  __-%c%c=======.--%c%c  %c%c--.=================================.--%c%c  %c%c--.=======:\n", 34, 34, 34, 34, 34, 34, 34, 34, 34, 34);
   printf(" ]       [w] : /        %c : |========================|    : /        %c :  [w] :\n", 92, 92);
   printf(" V___________:|          |: |========================|    :|          |:   _-%c\n", 34);
   printf("  V__________: %c        / :_|=======================/_____: %c        / :__-%c\n", 92, 92, 34);
   printf("  -----------'  %c%c____%c%c  `-------------------------------'  %c%c____%c%c\n\n", 34, 34, 34, 34, 34, 34, 34, 34);
   printf("\n\n\n");
   printf("\n                                   LOADING\n");
   printf("                                 ");

   for (counter=0;counter<5;counter++)
   {
      for (count=0; count<2; count++)
      {

         printf("%c", 219);

      }
   }

}

void add_customer(FILE *cfPtr)
{
   int num_service;//variable to accept number of services

   CUSTOMER customer = {"", "", "", "", "", "", "", "", {""}, 0, 0.0};

   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{

   printf("          CUSTOMER INFORMATION");

   printf("  [NEW!]");
   printf("\n\n");

   printf("          Date of Birth Format : dd/mm/yyyy");
   printf("\n\n");
   printf("          Customer-ID   : ");
   scanf("%d", &customer.c_id);//scans customer's id into file
   fflush(stdin);

   if(customer.c_id <= 0 || customer.c_id > 100)
   {
      printf("          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
   printf("          First Name    : ");
   scanf("%s", &customer.fname);//scans customer's first name into file
   fflush(stdin);
   printf("\n");
   printf("          Last Name     : ");
   scanf("%s", &customer.lname);//scans customer's last name into file
   fflush(stdin);
   printf("\n");
   printf("          Date of Birth : ");
   scanf("%s", &customer.dob);//scans customer's last name into file
   printf("\n");
   fflush(stdin);
   printf("          Phone 1       : ");
   scanf("%s", &customer.land_line);//scans customer's home phone number into file
   fflush(stdin);
   printf("\n");
   printf("          Phone 2       : ");
   scanf("%s", &customer.cell_phone);//scans customer's cellular phone number into file
   fflush(stdin);
   printf("\n");
   printf("          Address 1     : ");
   scanf("%s", &customer.address_line1);//scans customer's street address into file
   fflush(stdin);
   printf("\n");
   printf("          Address 2     : ");
   scanf("%s", &customer.address_line2);//scans customer's city into file
   fflush(stdin);
   printf("\n");
   printf("          Email         : ");
   scanf("%s", &customer.email);//scans customer's emaail into file
   fflush(stdin);
   printf("\n");
   printf("          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:
           printf("\n");
           printf("          Service       : ");
           scanf("%s", &customer.serv_type.service1);//scans service the customer solicitted
           fflush(stdin);
           break;
   case 2:
           printf("\n");
           printf("          Service [1]    : ");
           scanf("%s", &customer.serv_type.service1);
           printf("          Service [2]    : ");
           scanf("%s", &customer.serv_type.service2);
           fflush(stdin);
           break;
   case 3:
           printf("\n");
           printf("          Service [1]    : ");
           scanf("%s", &customer.serv_type.service1);
           printf("          Service [2]    : ");
           scanf("%s", &customer.serv_type.service2);
           printf("          Service [3]    : ");
           scanf("%s", &customer.serv_type.service3);;
           fflush(stdin);
           break;
   case 4:
           printf("\n");
           printf("          Service [1]    : ");
           scanf("%s", &customer.serv_type.service1);
           printf("          Service [2]    : ");
           scanf("%s", &customer.serv_type.service2);
           printf("          Service [3]    : ");
           scanf("%s", &customer.serv_type.service3);
           printf("          Service [4]    : ");
           scanf("%s", &customer.serv_type.service4);
           fflush(stdin);
           break;
   case 5:
           printf("\n");
           printf("          Service [1]    : ");
           scanf("%s", &customer.serv_type.service1);
           printf("          Service [2]    : ");
           scanf("%s", &customer.serv_type.service2);
           printf("          Service [3]    : ");
           scanf("%s", &customer.serv_type.service3);
           printf("          Service [4]    : ");
           scanf("%s", &customer.serv_type.service4);
           printf("          Service [5]    : ");
           scanf("%s", &customer.serv_type.service5);
           fflush(stdin);
           break;
   default:
           printf("\n");
           printf("          Enter a valid option!");
           break;
   }
   printf("\n");

   printf("          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);

   printf("          File sucessfully created!");
   printf("\n\n");
   printf("          Press any key to continue...");
   getch();
   fclose(cfPtr);
   }
   menu();
}

void delete_customer(FILE *cfPtr)
{
   CUSTOMER customer;
   CUSTOMER blankCustomer = {"", "", "", "", "", "", "", "", {""}, 0, 0.0};

   int cust_id;

   if((cfPtr = fopen("ajcustomer.dat", "rb+"))== NULL)
   {

       printf("The Customer File could not be opened.");
      printf("\n");
   }
   else {

   printf("          CUSTOMER INFORMATION");

   printf("  [DELETE!]");
   printf("\n\n");

   printf("          Enter the Customer-ID of the Customer you wish to delete.");
   printf("\n\n");
   printf("          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)
   {

      printf("\n          That Customer account does not exist.\n");
   }
   else
   {
      fseek(cfPtr, (cust_id - 1) * sizeof(CUSTOMER), SEEK_SET);
      fwrite(&blankCustomer, sizeof(CUSTOMER), 1, cfPtr);

      printf("\n\n          The Customer account has been successfully deleted.", customer.c_id);
      printf("\n\n");
      printf("          Press any key to continue...");
      getch();
   }
   fclose(cfPtr);
   }
   menu();
}

/*void edit_customer(FILE *cfPtr)
{

} */

.

first off, the very first thing i've found is that your "add customer" function, which you said is working, is in fact not working.

i entered sample data for two of examples, and the resulting text as stored in the "ajcustomer.dat" is completely mangled and missing more than half of the information.

part of this problem is your use of, for example, scanf("%s", &customer.address_line1); does not allow spaces in the code.

so if my address is entered as "22 Acacia Avenue", it loses everything but the "22"

inspect your "ajcustomer.dat" and you will see what i mean.

you really need to have the precise format of this data file specified before you begin, and make absoultely certain that all of your fixed-width fields are indeed being entered exactly as you think they are supposed to be.

i strongly suggest that you get rid of all instances of "scanf()" and replace them with "fgets()" to read from STDIN

another thing you dont need to do is open the file as a binary file. this is a text file, just open it normally.

ohh yess i changed it to gets for that problem, I'm not sure how to space out everything in the file exactly lol I was just happy that I got it to add at the time...I dont think she exactly wants everything in the file to look presentable she just want to kno that it adds and deletes

do NOT use "gets" ... use "fgets" ... using "gets" is probably the single worst programmatical error you could ever make. that command should be cut out of the C Language like a malignant tumor. Truly, it's even worse than "fflush(stdin)"

and look. i quite think your instructor WILL expect your text file that contains your customer data to be accurate.

(1) if its not accurate, you've lost data. In the "real world" that's MUCH WORSE than not having a program work at all.

(2) how can you delete information if the information is mangled and written in a semi-random manner? you can't even know where the information is in order to delete it.

your delete function is not working partially becasue your add function is not working. for one thing youre not even writing the customer ID to your file. How can you find the customer record if you're not even writing the ID?

one solution to that problem is to make the customer ID a string, not an integer.

another major problem with your delete function is that the "blank record" or whatever you call it that youre trying to use to overwrite the customer record you want to delete... it's all just empty strings. as in "zero length" empty strings.

you need to write strings of NULL characters (0x00) of a length equal to the fields youre trying to wipe out.... or write blank spaces, if your program does not specifically require null characters to indicate an empty field. the details don't matter here.... the point is, you're just not overwriting the existing file.

It comes back to this: for this to EVER work, your text file needs to be precisely defined into fixed fields that are always at the same relative locations. you need to get very familiar with your "ajcustomer.dat" file and understand exactly what is being written there, and why.


.

true thanx much for all your help...how exactly do u use fgets wud u mind giving an example please , consider me the noobiest noob ever we are just doing basic C programming really we didnt even get taught files so I had to research myself but I guess i'm failing

here's an example of fgets(). fgets has two important distinctions. it allows you to choose the stream (stdin or file), and requires you to indicate how many characters will be accepted. gets will not check and will cause the entire program to crash if user enters too many.

ive combined it with sscanf() which is different than scanf because it scans a string, and is more easy to control, IMO.

char menu_str[3]; // what the user enters (can be anything)
   int menu_valid;  // is the user entry valid?
   int menu_option; //  user selection converted to decimal    
   FILE *cPtr, *crPtr; // pointer to ajcustomer and ajservice files

   // ...

   menu_valid = 0;  // assume invalid until proven otherwise
   while (!menu_valid)
   {
      fgets(menu_str,2,stdin);//accepts two chars for user's option
      menu_valid = sscanf(menu_str,"%u",&menu_option); // look for unsigned decimal
   }

   // menu_str has been converted to integer
   if(menu_option != 0)
   {

      switch(menu_option)
//...

one thing... dont take the criticism personally. everyone here was a beginner once and we all made the same exact mistakes.

i also want to say that that i'm impressed by the artistic skill and detail you've put into this. it's quite impressive -- even though i despise Borland's nonstandard C code that gets you there.

it's good stuff. because you can get a job developing user interfaces. i know a guy at microsoft who that's all he does, and hes quite successful.

(still, do remember that a broken program, no matter how pretty, is still broken.)

but you're learning. and trying. so it's all good.


.

ohh yea i totally understand I really didn't start working on the interface till after because at one point though the file didn't write perfectly it did write to it and it did delete the records I specified so I just left it and started on the interface since it seemed to be okay but I really don't know what happened and everything went all wrong so I decided to start over the functions but that didn't seem to help so basically all I'm left with is the interface and malfunctioning add and delete functions lol

I am going to try out the fgets now, I really appreciate all your help though I don't take the criticism personally lol I always say Jamaica is truly behind in the computer world

o wait.... i see what you're trying to do...

you're trying to write the binary values of the structure itself to a file.

i just assumed you were writing the strings themselves.

hmm. kind of changes things.

so I shouldn't use fgets?

gah.

i tried to open in a different debugging environment, now it's barfing on me because you're using all those scanfs expecting a pointer to char, but writing to structure aggregate.

not to mention your highly non-canonical use of "getch" . all these non-standard C causing undefined behavior is killing me.

man, i'm sorry, i thought i could just strip the borland terminal graphics stuff out, but just theres too much work here to undo this to be even remotely portable.

and i've got to go home now, and i know this wont fly on my linux machine.


good luck

so I shouldn't use fgets?

No, you should definitely use fgets.

i just meant it changed things for my understanding of how you were writing your data to the file.

do continue to use the "binary" mode for reading and writing you will need that.

but i tried to run this in my debugging environment in order to inspect how the delete was working, and it just pretty much crapped the bed.


it's not your fault. youre being forced to work on a platform that no one else in developed nations uses. Borland is a scam, in my opinion, they sell their crap really cheap, dirt cheap, to schools in places like your country and India and others, and get people working on all this non-standard crap.

then you just have to unlearn it if you ever want to work anywhere that's not a local extension of your college, and you're pretty much prevented from collaborating with anyone that works in the standardized industries.

Borland sucks.

I'm sorry you have to deal with it.

.

ohhh drat...well I thank you once again for all your help and I will implement the fgets and see if it fixes some of the bugginess and I'm gonna try and do some more researching on it hopefully get it atleast half functional for tommorow.....THANK YOU I APPRECIATE ALL YOUR HELP:)

yeah. i wish i had less complaints about borland, and more solutions for you

:(

last bit of advice.... use your debug environment to step through your code one line at a time, and watch the variables in question.

thats really the only way to fix this.

it's okay lol borland is crappy but they require us to use it for whatever reason....I'll try that stepping...I sure hope she gives an extention on this though ... safe trip home ty again :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.