Code:
#include <stdio.h>
#include <string.h>
//#include <stdlib.h> //probably don't need this either
//You don't need these two files IMO.
//#include <windows.h>
//#include <conio.h>
struct customer
{
int custID;
char custName[50];
char custAddress[100];
char custBday[20];
char custProdBought[20];
char custDateProdBought[20];
int custProdPrice;
};
typedef struct customer c;
void createNew(void); //initialize your file
void addRecord(c c1[30]); // add a new record to the file
void updateRecord(void); // select a record and make changes
void displayFile(void);// points the record in the file in the order they are entered
void searchFile(void); //determine if the record is in the file
void recordCount(c c1[30], int *count); // determine how many record are in the file
void sortRecord(c c1[30], int size); // arrange the record in the file(key specified)
void exit1();
void createNew()
{
FILE *fp;
fp=fopen("Customer.dat", "r");
if(fp==NULL)
printf("File creation Failed!\n");
else
printf("File created!\n");
fclose(fp);
}
void addRecord(c c1[30])
{
int i,n,cc=0;
FILE *fp;
fp=fopen("Customer.dat", "a");
system("cls");
if(fp==NULL)
{
printf("File Creation Failed!");
}
system("cls");
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 : ");
gets(c1[i].custName);
printf("Customer's Address : ");
gets(c1[i].custAddress);
printf("Customer's Birthday : ");
gets(c1[i].custBday);
printf("Customer's Last Product Bought: ");
gets(c1[i].custProdBought);
printf("Date Bought : ");
gets(c1[i].custDateProdBought);
printf("Product's Price : ");
scanf("%d", &c1[i].custProdPrice);
//gets(c[i].custProdPrice); //this is an int, not a string
fwrite(&c1[i], sizeof(c), 1, fp);
}cc++;
fclose(fp);
}
void updateRecord()
{
int recno, nofrec=0;
char ch;
struct customer cust1;
FILE *fp;
fp=fopen("Customer.dat", "rb+");
system("cls");
printf("Enter the Customer's ID to modify : ");
scanf("%d", &recno);
(void) getchar(); //remove the newline from the keyboard stream
while((fread(&cust1, sizeof(cust1), 1, fp))==1)
{
nofrec++;
if(cust1.custID==recno)
{
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("Customer's Birthday: %c.\n", cust1.custBday);
printf("Customer's Last Product Bought: %c.\n",cust1.custProdBought);
printf("Date bought: %c.\n",cust1.custDateProdBought);
printf("Product's Price: %c.\n",cust1.custProdPrice);
printf("\n");
printf("Do you want to modify this record?(Y/N): ");
scanf("%c", &ch);
(void) getchar();
fseek(fp, ((nofrec-1)*sizeof(cust1)), 0);
if(ch=='Y'|| ch=='y')
{
printf("Enter customer's last product bought: ");
scanf("%c",&cust1.custProdBought);
(void) getchar();
printf("Enter date bought: ");
scanf("%c", &cust1.custDateProdBought);
(void) getchar();
printf("Enter the product's price: ");
scanf("%c", &cust1.custProdPrice);
(void) getchar();
fwrite(&cust1, sizeof(cust1), 1, fp);
printf("Record was successfully modified.");
}
else
printf("No modifications were made.");
fclose(fp);
}
}
}
void displayFile()
{
int nofrec=0;
c cust1;
FILE *fp;
fp=fopen("Customer.dat", "rb");
if(fp==NULL)
{
printf("\n\tFile doesn’t exist!!!\nTRY 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("Customer's Birthday: %c.\n", cust1.custBday);
printf("Customer's Last Product Bought: %c.\n",cust1.custProdBought);
printf("Date bought: %c.\n",cust1.custDateProdBought);
printf("Product's Price: %c.\n",cust1.custProdPrice);
printf("\n\n\n");
}
printf("Total number of records present are : %d", nofrec);
fclose(fp);
}
void searchFile()
{
int s,recno;
char sname[20];
c cust1;
FILE *fp;
fp=fopen("Customer.dat", "rb");
system("cls");
printf("What do you want to search for?\n");
printf("[1] Search by Customer's Name.");
printf("[2] Search by Customer's ID.");
scanf("%d", &s);
system("cls");
switch(s)
{
case 1: printf("Enter the Customer's name to search: ");
fflush(stdin);
gets(sname);
while((fread(&cust1, sizeof(cust1), 1, fp))==1)
{
if(strcmp(sname,cust1.custName)==0)
{
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("Customer's Birthday: %c.\n", cust1.custBday);
printf("Customer's Last Product Bought: %c.\n",cust1.custProdPrice);
printf("Date bought: %c.\n",cust1.custDateProdBought);
printf("Product's Price: %c.\n",cust1.custProdPrice);
printf("\n");
}
}
break;
case 2: printf("Enter the Customer's ID to search :" );
scanf("%d", &recno);
while((fread(&cust1, sizeof(c), 1, fp))==1)
{
if(cust1.custID==recno)
{
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("Customer's Birthday: %c.\n", cust1.custBday);
printf("Customer's Last Product Bought: %c.\n",cust1.custProdPrice);
printf("Date bought: %c.\n",cust1.custDateProdBought);
printf("Product's Price: %c.\n",cust1.custProdPrice);
printf("\n");
}
}
break;
}
}
void recordCount(c c1[30], int *count)
{
addRecord(c1);
count =0;
count++;
}
void sortRecord(c c1[30], int size)
{
int x, y;
c temp;
for(x=0 ; x < size - 1 ; x++)
{
for(y=0; y < size - 1 ; y++)
{
if(strcmp(c1[y].custName, c1[y+1].custName) > 0)
{
temp = c1[y];
c1[y]= c1[y+1];
c1[y+1]= temp;
}
}
}
}
void exit1()
{
system("cls");
printf("Goodbye!");
}
int main()
{
c c1[30];
int a,i;
char ch;
printf("Creating file...\n");
createNew();
printf("Press enter to continue...");
getch();
system("cls");
do{
printf("\nCustomer's Information Data.\n\n");
printf("What do you want to do?\n");
printf("[1] Add new Customer Information.\n");
printf("[2] Update a Customer Information.\n");
printf("[3] Display list of record.\n");
printf("[4] Search a record.\n");
printf("[5] Exit.\n\n");
printf("\n\n");
recordCount(c1, &i);
printf("\n\n");
sortRecord(c1,i);
do
{
scanf("%d",&a);
(void) getchar(); //clear the newline from the input stream
switch(a)
{
case 1: addRecord(c1);break;
case 2: updateRecord();break;
case 3: displayFile();break;
case 4: searchFile();break;
case 5: exit1();
default: printf("Invalid Choice!");
}
}
while (a<0 && a>7);
printf("\nDo you want to try again(Y/N)? :");
scanf("%c", &ch);
}
while(ch=='y'|| ch=='Y');
return 0;
}
It's not good code yet, but it will compile at least. c1 is the name of the array now.