Code:
#include <stdio.h>
#include
#include <string.h>
#include
FILE *fSupplier;
void DisplaySupplierMenu();
void SearchBySupplierCode();
void AddSupplierRecord();
void AmendSupplierRecord();
bool SearchSupplier();
void DisplaySearchedSupplierRecord();
bool WriteNewSupplierRecord();
void LoadSupplierRecord(struct SupplierRefer **q);
void AddSupplierNode(struct SupplierRefer **q, int num);
void AssignSupplierRefer(struct SupplierRefer **p);
void OpenSupplierFile();
void CloseSupplierFile();
int CurrSupplierRecNum=0;
int TotalSupplierNode=0;
char SearchedSupplierCode[7];
struct Supplier{
char SupplierCode[7];
char SupplierName[21];
char SupplierAddress[21];
char SupplierPhone[11];
};
struct Supplier Supplier;
struct SupplierRefer{
char SupplierCode[7];
struct SupplierRefer *Supplierlink;
};
struct SupplierRefer *mySupplierrefer;
int main() {
mySupplierrefer=(struct SupplierRefer*)malloc(sizeof(struct
SupplierRefer));
LoadSupplierRecord(&mySupplierrefer);
bool isExit=false;
while (!isExit) {
DisplaySupplierMenu();
char ch;
gets(&ch);
switch (ch) {
case '1':
SearchBySupplierCode();
break;
case '2':
AddSupplierRecord();
break;
case '3':
AmendSupplierRecord();
break;
case '0':
isExit=true;
}
}
return 0;
}
void DisplaySupplierMenu() {
puts(" \n");
puts(" * 1 : View Supplier Record *\n");
puts(" * 2 : Add Supplier Record *\n");
puts(" * 3 : Amend Supplier Record *\n");
puts(" * 0 : Exit *\n");
puts(" \n");
}
void OpenSupplierFile() {
fSupplier=fopen("a:\\Supplier.txt", "r+");
if (fSupplier==0) {
fSupplier=fopen("a:\\Supplier.txt", "w");
fclose(fSupplier);
fSupplier=fopen("a:\\Supplier.txt", "r+");
}
}
void CloseSupplierFile() {
fclose(fSupplier);
}
void SearchBySupplierCode() {
OpenSupplierFile();
puts("Enter Supplier Code For The Record You Want To
Search\n");
gets(SearchedSupplierCode);
if (SearchSupplier()) {
DisplaySearchedSupplierRecord();
CloseSupplierFile();
return;
}
else {
printf("Supplier Code Wasn't Found");
return;
}
}
void DisplaySearchedSupplierRecord() {
long offset;
printf("Searched Record Details\n");
offset=sizeof(Supplier)* CurrSupplierRecNum;
fseek(fSupplier,offset,0);
fread(&Supplier, sizeof(Supplier), 1, fSupplier);
puts("\nSupplier Code");
puts(Supplier.SupplierCode);
puts("\nSupplier Name");
puts(Supplier.SupplierName);
puts("\nSupplier Address");
puts(Supplier.SupplierAddress);
puts("\nSupplier Phone");
puts(Supplier.SupplierPhone);
printf("\nSearched Successfully");
}
void AddSupplierRecord() {
puts("\nEnter New Supplier Code\n");
gets(SearchedSupplierCode);
OpenSupplierFile();
while(true) {
if (SearchSupplier()) {
printf("\nEntered Supplier Code Already Exixts\n");
puts("\nEnter New Supplier Code\n");
gets(SearchedSupplierCode);
}
else {
if (WriteNewSupplierRecord()) {
AddSupplierNode(&mySupplierrefer, TotalSupplierNode);
CloseSupplierFile();
return;
}
else {
CloseSupplierFile();
return;
}
}
}
CloseSupplierFile();
}
bool WriteNewSupplierRecord() {
long offset;
char temp_name[21];
char temp_address[21];
char temp_phone[11];
offset=sizeof(Supplier)* TotalSupplierNode;
fseek(fSupplier, offset, 0);
fflush(stdin);
if(strlen(SearchedSupplierCode)== 6) {
strcpy(Supplier.SupplierCode, SearchedSupplierCode);
puts("Enter Value For Supplier Name, Should Have Atleast
Single Character\n");
gets(temp_name);
if(strlen(temp_name)>0 & strlen(temp_name)<21) {
strcpy(Supplier.SupplierName, temp_name);
puts("Enter Value For Supplier Address\n");
gets(temp_address);
if(strlen(temp_address)>0 & strlen(temp_address)<21) {
strcpy(Supplier.SupplierAddress, temp_address);
puts("Enter Value For Supplier Phone Number, Should Be
Between 1-10 Characters");
gets(temp_phone);
if(strlen(temp_phone)>0 & strlen(temp_phone)<11) {
strcpy(Supplier.SupplierPhone, temp_phone);
fwrite(&Supplier, sizeof(Supplier), 1, fSupplier);
puts("New Record Added Successfully");
return true;
}
else {
puts("Enterd Supplier Phone Number Is Not Valid, Should
Be Between 1-10 Characters");
return false;
}
}
else {
puts("Enterd Supplier Address Is Not Valid, Should Be
Between 1-20 Characters");
return false;
}
}
else {
puts("Enterd Supplier Name Is Not Valid, Should Be Between
1-20 Characters, Should Have Atleast Single
Character");
AddSupplierRecord();
}
}
else {
puts("Enterd Supplier Code Is Not Valid, Should Be 6
Characters\n");
AddSupplierRecord();
}
}
void AmendSupplierRecord() {
long offset;
char temp_name[21];
char temp_address[21];
char temp_phone[11];
fflush(stdin);
puts("\nEnter Supplier Code You Want To Amend\n");
gets(SearchedSupplierCode);
OpenSupplierFile();
if (SearchSupplier()) {
offset=sizeof(Supplier)* CurrSupplierRecNum;
fseek(fSupplier, offset, 0);
fread(&Supplier, sizeof(Supplier), 1, fSupplier);
puts(Supplier.SupplierName);
puts("\nEnter New Value For Supplier Name\n");
gets(temp_name);
if(strlen(temp_name)>0 & strlen(temp_name)<21)
strcpy(Supplier.SupplierName, temp_name);
else {
printf("Entered Supplier Name Is Not Valid, Should Be
Between 1-20 Characters");
return;
}
puts(Supplier.SupplierAddress);
puts("Enter New Value For Supplier Address\n");
gets(temp_address);
if(strlen(temp_address)>0 & strlen(temp_address)<21)
strcpy(Supplier.SupplierAddress, temp_address);
else {
printf("Entered Supplier Address Is Not Valid, Should Be
Between 1-20 Characters");
return;
}
puts(Supplier.SupplierPhone);
puts("Enter New Value For Supplier Phone Number\n");
gets(temp_phone);
if(strlen(temp_phone)>0 & strlen(temp_phone)<11)
strcpy(Supplier.SupplierPhone, temp_phone);
else {
printf("Entered Supplier Phone Number Is Not Valid, Should
Be Between 1-10 Characters");
return;
}
fseek(fSupplier, offset, 0);
fwrite(&Supplier, sizeof(Supplier),1,fSupplier);
fflush(stdin);
puts("Record Successful Amended");
CloseSupplierFile();
LoadSupplierRecord(&mySupplierrefer);
}
else {
printf("Searched Supplier Code Does Not Exist");
fflush(stdin);
}
}
bool SearchSupplier() {
int index=0;
struct SupplierRefer *temp;
temp=mySupplierrefer->Supplierlink;
if (TotalSupplierNode==0 ) {
puts("File Is Empty.");
return false;
}
while (index<=TotalSupplierNode-1) {
if (strcmp(SearchedSupplierCode,temp->SupplierCode)==0) {
CurrSupplierRecNum=index;
return true;
}
else {
index+=1;
temp=temp->Supplierlink;
}
}
return false;
}
bool FindSupplierName() {
OpenSupplierFile();
return false ;
}
void LoadSupplierRecord(struct SupplierRefer **q) {
long offset;
struct SupplierRefer *temp;
temp=*q;
TotalSupplierNode=0;
OpenSupplierFile();
while(true) {
if(fread(&Supplier, sizeof(Supplier),1,fSupplier)==1) {
if (temp==NULL) {
temp=(struct SupplierRefer*)malloc(sizeof(struct
SupplierRefer));
AssignSupplierRefer(&temp);
}
else {
temp->Supplierlink=(struct SupplierRefer*)malloc(sizeof
(struct SupplierRefer) );
temp=temp->Supplierlink;
AssignSupplierRefer(&temp);
}
}
else {
CloseSupplierFile();
break;
}
}
return;
}
void AssignSupplierRefer(struct SupplierRefer **p) {
struct SupplierRefer *temp;
temp=*p;
long offset=sizeof(Supplier)*TotalSupplierNode;
fseek(fSupplier, offset, 0);
fread(&Supplier, sizeof(Supplier), 1,fSupplier);
strcpy(temp->SupplierCode,Supplier.SupplierCode);
TotalSupplierNode+=1;
return;
}
void AddSupplierNode(struct SupplierRefer **q, int num) {
int index;
struct SupplierRefer *temp;
if (TotalSupplierNode==0 || *q==NULL) {
*q=(struct SupplierRefer*)malloc(sizeof(struct SupplierRefer));
temp=*q;
AssignSupplierRefer(&temp);
return;
}
else {
while(index<=TotalSupplierNode) {
temp=temp->Supplierlink;
}
temp=(struct SupplierRefer*)malloc(sizeof(struct
SupplierRefer) );
AssignSupplierRefer(&temp);
}
return;
}