Can anybody help? I am trying to build a Bank Management System but I am suppose
ID: 3695869 • Letter: C
Question
Can anybody help? I am trying to build a Bank Management System but I am supposed to use the code below to create it. It is all in C . I must adapt the code below, it was initiallhy used for a phonebook directory!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool int;
#define true 1
#define false 0
struct contactStruct {
int ID;
char fName[20];
char lName[20];
char pNumber[15];
char email[50];
char address[100];
};
typedef struct contactStruct contactStrt;
// Read a struct assuming fields are separated by space
void readStruct(contactStrt * cStruct, FILE* fHandle)
{
fscanf(fHandle, "%d", (*cStruct).ID);
fscanf(fHandle, "%s", (*cStruct).fName);
fscanf(fHandle, "%s", (*cStruct).lName);
fscanf(fHandle, "%s", (*cStruct).pNumber);
fscanf(fHandle, "%s", (*cStruct).email);
fscanf(fHandle, "%s", (*cStruct).address);
}
// write a struct to the file assuming fields are separated by space
void writeStruct(contactStrt* cStruct, FILE* fHandle)
{
fprintf(fHandle, "%d,", (*cStruct).ID);
fprintf(fHandle, "%s,", (*cStruct).fName);
fprintf(fHandle, "%s,", (*cStruct).lName);
fprintf(fHandle, "%s,", (*cStruct).pNumber);
fprintf(fHandle, "%s,", (*cStruct).email);
fprintf(fHandle, "%s", (*cStruct).address);
fprintf(fHandle, "%s", " ");
}
// reads one line splits on commas and puts into a struct
bool readLine(contactStrt * cStruct, FILE* fHandle)
{
char line[255];
char delim[2] = ",";
char *token;
if (fgets(line, 254, fHandle) != NULL) // read one line up to 254 charactes and put it in line!
{
// Tokenize it
token = strtok(line, delim);
(*cStruct).ID = atoi(token);
//strcpy((*cStruct).ID, token);
token = strtok(NULL, delim);
strcpy((*cStruct).fName, token);
token = strtok(NULL, delim);
strcpy((*cStruct).lName, token);
token = strtok(NULL, delim);
strcpy((*cStruct).pNumber, token);
token = strtok(NULL, delim);
strcpy((*cStruct).email, token);
token = strtok(NULL, delim);
strcpy((*cStruct).address, token);
(*cStruct).address[strlen((*cStruct).address) - 1] = '';
return true;
}
else
{
return false;
}
}
// Writes one line in the file
void writeLine(contactStrt * cStruct, FILE* fHandle)
{
char line[255] = "";
char id[50];
_itoa((*cStruct).ID, id, 10);
strcat(line, id);
strcat(line, ",");
strcat(line, (*cStruct).fName);
strcat(line, ",");
strcat(line, (*cStruct).lName);
strcat(line, ",");
strcat(line, (*cStruct).pNumber);
strcat(line, ",");
strcat(line, (*cStruct).email);
strcat(line, ",");
strcat(line, (*cStruct).address);
strcat(line, " ");
fprintf(fHandle, "%s", line);
}
// reads the whole file and populates list of contacts in contactsList
// it also keeps track of number of contacts read from the file in
// numOfContacts
void readFile(contactStrt contactsList[50], int* numOfContacts, FILE* fHandle)
{
*numOfContacts = 0;
while (readLine(&contactsList[*numOfContacts], fHandle) == true)
{
*numOfContacts = *numOfContacts + 1;
}
}
void writeFile(contactStrt contactsList[50], int numOfContacts, FILE* fHandle)
{
int i;
for (i = 0; i < numOfContacts; i = i + 1)
{
writeLine(&contactsList[i], fHandle);
}
}
// Search contacts using first name
void searchFirstName(contactStrt contactsList[50], int numOfContacts,
char fName[50], contactStrt resultList[50], int * numOfResult)
{
int ind;
*numOfResult = 0;
for (ind = 0; ind < numOfContacts; ind = ind + 1)
{
if (strcmp(fName, contactsList[ind].fName) == 0)
{
resultList[*numOfResult].ID = contactsList[ind].ID;
strcpy(resultList[*numOfResult].fName, contactsList[ind].fName);
strcpy(resultList[*numOfResult].lName, contactsList[ind].lName);
strcpy(resultList[*numOfResult].pNumber, contactsList[ind].pNumber);
strcpy(resultList[*numOfResult].address, contactsList[ind].address);
strcpy(resultList[*numOfResult].email, contactsList[ind].email);
*numOfResult = *numOfResult + 1;
}
}
}
void printSearchResult(contactStrt resultList[50], int * numOfResult)
{
int ind;
for (ind = 0; ind < *numOfResult; ind = ind + 1)
{
printf(" %d %s %s %s %s %s ", resultList[ind].ID, resultList[ind].fName
, resultList[ind].lName, resultList[ind].pNumber, resultList[ind].email
, resultList[ind].address);
}
}
int main()
{
// Variables
char inFileName[255] = "contacts.txt"; // file names in windows can't be longer than 255 characters!
char outFileName[255] = "copy.txt";
FILE* fHandle = NULL; // File handler
contactStrt cStruct;
contactStrt contactsList[50];
int numOfContacts;
printf("Openning the file in read mod... ");
fHandle = fopen(inFileName, "r");
if (fHandle == NULL)
{
printf("ERR! Unable to open input file. ");
return false;
}
printf("Reading from the file... ");
//readStruct(&cStruct, fHandle);
//readLine(&cStruct, fHandle);
readFile(contactsList, &numOfContacts, fHandle);
printf("Closing and openning the file in write mod... ");
fclose(fHandle);
contactStrt resultList[50];
int numOfResult;
char fName[50];
printf("Please enter a first name: ");
scanf("%s", fName);
searchFirstName(contactsList, numOfContacts, fName, resultList, &numOfResult);
printSearchResult(resultList,&numOfResult);
fHandle = fopen(outFileName, "w");
if (fHandle == NULL)
{
printf("ERR! Unable to open input file. ");
return false;
}
//writeStruct(&cStruct, fHandle);
//writeLine(&cStruct, fHandle);
writeFile(contactsList, numOfContacts, fHandle);
printf("Closing the file. ");
fclose(fHandle);
return 0;
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool int;
#define true 1
#define false 0
struct contactStruct {
int ID;
char fName[20];
char lName[20];
char pNumber[15];
char email[50];
char address[100];
};
typedef struct contactStruct contactStrt;
// Read a struct assuming fields are separated by space
void readStruct(contactStrt * cStruct, FILE* fHandle)
{
fscanf(fHandle, "%d", (*cStruct).ID);
fscanf(fHandle, "%s", (*cStruct).fName);
fscanf(fHandle, "%s", (*cStruct).lName);
fscanf(fHandle, "%s", (*cStruct).pNumber);
fscanf(fHandle, "%s", (*cStruct).email);
fscanf(fHandle, "%s", (*cStruct).address);
}
// write a struct to the file assuming fields are separated by space
void writeStruct(contactStrt* cStruct, FILE* fHandle)
{
fprintf(fHandle, "%d,", (*cStruct).ID);
fprintf(fHandle, "%s,", (*cStruct).fName);
fprintf(fHandle, "%s,", (*cStruct).lName);
fprintf(fHandle, "%s,", (*cStruct).pNumber);
fprintf(fHandle, "%s,", (*cStruct).email);
fprintf(fHandle, "%s", (*cStruct).address);
fprintf(fHandle, "%s", " ");
}
// reads one line splits on commas and puts into a struct
bool readLine(contactStrt * cStruct, FILE* fHandle)
{
char line[255];
char delim[2] = ",";
char *token;
if (fgets(line, 254, fHandle) != NULL) // read one line up to 254 charactes and put it in line!
{
// Tokenize it
token = strtok(line, delim);
(*cStruct).ID = atoi(token);
//strcpy((*cStruct).ID, token);
token = strtok(NULL, delim);
strcpy((*cStruct).fName, token);
token = strtok(NULL, delim);
strcpy((*cStruct).lName, token);
token = strtok(NULL, delim);
strcpy((*cStruct).pNumber, token);
token = strtok(NULL, delim);
strcpy((*cStruct).email, token);
token = strtok(NULL, delim);
strcpy((*cStruct).address, token);
(*cStruct).address[strlen((*cStruct).address) - 1] = '';
return true;
}
else
{
return false;
}
}
// Writes one line in the file
void writeLine(contactStrt * cStruct, FILE* fHandle)
{
char line[255] = "";
char id[50];
_itoa((*cStruct).ID, id, 10);
strcat(line, id);
strcat(line, ",");
strcat(line, (*cStruct).fName);
strcat(line, ",");
strcat(line, (*cStruct).lName);
strcat(line, ",");
strcat(line, (*cStruct).pNumber);
strcat(line, ",");
strcat(line, (*cStruct).email);
strcat(line, ",");
strcat(line, (*cStruct).address);
strcat(line, " ");
fprintf(fHandle, "%s", line);
}
// reads the whole file and populates list of contacts in contactsList
// it also keeps track of number of contacts read from the file in
// numOfContacts
void readFile(contactStrt contactsList[50], int* numOfContacts, FILE* fHandle)
{
*numOfContacts = 0;
while (readLine(&contactsList[*numOfContacts], fHandle) == true)
{
*numOfContacts = *numOfContacts + 1;
}
}
void writeFile(contactStrt contactsList[50], int numOfContacts, FILE* fHandle)
{
int i;
for (i = 0; i < numOfContacts; i = i + 1)
{
writeLine(&contactsList[i], fHandle);
}
}
// Search contacts using first name
void searchFirstName(contactStrt contactsList[50], int numOfContacts,
char fName[50], contactStrt resultList[50], int * numOfResult)
{
int ind;
*numOfResult = 0;
for (ind = 0; ind < numOfContacts; ind = ind + 1)
{
if (strcmp(fName, contactsList[ind].fName) == 0)
{
resultList[*numOfResult].ID = contactsList[ind].ID;
strcpy(resultList[*numOfResult].fName, contactsList[ind].fName);
strcpy(resultList[*numOfResult].lName, contactsList[ind].lName);
strcpy(resultList[*numOfResult].pNumber, contactsList[ind].pNumber);
strcpy(resultList[*numOfResult].address, contactsList[ind].address);
strcpy(resultList[*numOfResult].email, contactsList[ind].email);
*numOfResult = *numOfResult + 1;
}
}
}
void printSearchResult(contactStrt resultList[50], int * numOfResult)
{
int ind;
for (ind = 0; ind < *numOfResult; ind = ind + 1)
{
printf(" %d %s %s %s %s %s ", resultList[ind].ID, resultList[ind].fName
, resultList[ind].lName, resultList[ind].pNumber, resultList[ind].email
, resultList[ind].address);
}
}
int main()
{
// Variables
char inFileName[255] = "contacts.txt"; // file names in windows can't be longer than 255 characters!
char outFileName[255] = "copy.txt";
FILE* fHandle = NULL; // File handler
contactStrt cStruct;
contactStrt contactsList[50];
int numOfContacts;
printf("Openning the file in read mod... ");
fHandle = fopen(inFileName, "r");
if (fHandle == NULL)
{
printf("ERR! Unable to open input file. ");
return false;
}
printf("Reading from the file... ");
//readStruct(&cStruct, fHandle);
//readLine(&cStruct, fHandle);
readFile(contactsList, &numOfContacts, fHandle);
printf("Closing and openning the file in write mod... ");
fclose(fHandle);
contactStrt resultList[50];
int numOfResult;
char fName[50];
printf("Please enter a first name: ");
scanf("%s", fName);
searchFirstName(contactsList, numOfContacts, fName, resultList, &numOfResult);
printSearchResult(resultList,&numOfResult);
fHandle = fopen(outFileName, "w");
if (fHandle == NULL)
{
printf("ERR! Unable to open input file. ");
return false;
}
//writeStruct(&cStruct, fHandle);
//writeLine(&cStruct, fHandle);
writeFile(contactsList, numOfContacts, fHandle);
printf("Closing the file. ");
fclose(fHandle);
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.