I have an assignment to do a customer application and I have managed to do the following code however, I know very little about files as we didn't do them in depth and whenever I add a new customer it doesn't get saved. Any suggestions please?
Code:#include <stdio.h> #include <stdlib.h> //for the clear screen function #include <string.h> #include <conio.h> //for getch() struct customer { int custID; char custName[50]; char custAddress[100]; }; typedef struct customer c; void load_menu(void); void customers_menu(void); void orders_menu(void); void stock_menu(void); void createNew(void); //initialize your file void add_Customer(c c1[30]); //add a new record to the file void recordCount(c c1[30], int *count); FILE *fp; int main(void) { /*FILE *pfile; pfile = fopen("customers.dat","w"); if (pFile!=NULL) { fputs ("fopen example",pFile); fclose (pFile); getchar(); // pause and wait for key } else { printf("Could not open the file. \n"); }*/ load_menu(); return 0; } void load_menu(void) { int choice; do { printf("Customer Orders Main Menu. \n\n"); printf("Please enter your choice: \n"); printf("1. Customer's Menu \n"); printf("2. Orders Menu\n"); printf("3. Product Stock Menu\n"); printf("4. Exit\n"); printf("\n"); if (scanf("%d",&choice)==1) { switch(choice) { case 1: system ("cls"); customers_menu(); printf("\n"); break; case 2: system ("cls"); orders_menu(); printf("\n"); break; case 3: system ("cls"); stock_menu(); printf("\n"); break; case 4: printf("Quitting program!\n"); break; default: printf("Invalid choice! Please try again\n"); printf("\n"); break; } } else { fflush(stdin); printf("Characters are invalid, please enter a number: \n "); choice=0; } }while((choice !=4)); } void createNew(void) { FILE *fp; fp=fopen("Customer.dat", "w"); if (fp==NULL) printf("File creation failed! \n"); else { printf("File created! \n"); printf("Press a key to continue \n"); fflush(stdout ); getchar(); fclose(fp); system ("cls"); } } void add_Customer(c c1[30]) { int i, n , cc=0; FILE *fp; fp=fopen("Customer.dat", "a"); system("cls"); if(fp==NULL) { printf("File Creation Failed!"); } printf("Enter the number of Customers: "); scanf("%d", &n); for(i=0;i<n;i++) { printf("Customer's ID (numbers only) : "); scanf("%d", &c1[i].custID); printf("Customer's Name: "); scanf("%s", c1[i].custName); printf("Customer's Address: "); scanf("%s", c1[i].custAddress); fwrite(&c1[i], sizeof(c), 1, fp); }cc++; fclose(fp); } void recordCount(c c1[30], int *count) { //add_Customer(c1); (if i use this, the program keeps looping whenever I enter a new customer count=0; count++; } void displayFile() { int nofrec=0; c cust1; FILE *fp; fp=fopen("Customer.dat", "rb"); if(fp==NULL) { printf("\n\tFile doesn’t exist!!!\n Plese try again."); } system("cls"); while((fread(&cust1, sizeof(cust1), 1, fp))==1) { nofrec++; printf("Customer's ID: %d.\n",cust1.custID); printf("Customer's Name: %c.\n",cust1.custName); printf("Customer's Address: %c.\n",cust1.custAddress); printf("\n\n\n"); } printf("Total number of records present are : %d", nofrec); fclose(fp); } void customers_menu(void) { int choice; c c1[30]; int i; do { printf("\n"); printf("Customers Menu \n\n"); printf("Please enter your choice: \n"); printf("1. Add Customer \n"); printf("2. Display File \n"); printf("3.\n"); printf("4. Go back to Main Menu \n"); recordCount (c1, &i); if (scanf("%d",&choice)==1) { switch(choice) { case 1: add_Customer(c1); createNew(); printf("\n"); break; case 2: displayFile(); printf("\n"); break; case 3: printf("\n"); break; case 4: printf("Going back to Main Menu\n"); system ("cls"); break; default: printf("Invalid choice! Please try again\n"); printf("\n"); break; } } else { fflush(stdin); printf("Characters are invalid, please enter a number: \n "); choice=0; } }while((choice !=4)); }



4Likes
LinkBack URL
About LinkBacks



