Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
struct clientData {
int accountNumb;
char lastName[15];
char firstName[10];
float money;
};
int makeChoice(void);
void textFile(FILE *);
void updateRegister(FILE *);
void insertRegister(FILE *);
void excludeRegister(FILE *);
int main(){
FILE *cfPtr;
int choice;
if ((cfPtr = fopen("credit.dat","r+")) == NULL)
printf("File Cannot be Openned\n");
else {
while ((choice = makeChoice()) != 5) {
switch (choice) {
case 1:
textFile(cfPtr);
break;
case 2:
updateRegister(cfPtr);
break;
case 3:
newRegister(cfPtr);
break;
case 4:
excludeRegister(cfPtr);
break;
}
}
}
fclose(cfPtr);
return 0;
}
void textFile(FILE *readPtr){
FILE *storePtr;
struct clientData client;
if ((storePtr = fopen("accounts.txt", "w")) == NULL)
printf("File cannot be openned\n");
else {
rewind(readPtr);
fprintf(storePtr,"%-6s%-16S%-11s%10s\n","Account" , "Last Name", "Name", "Credit");
while (!feof(readPtr)){
fread(&client, sizeof(struct clientData),1, readPtr);
if (client.accountNumb != 0)
fprint(storePtr, "%-6d%-16s%-11s%10.2f\n", client.accountNumb, client.lastName, client.firstName, client.money);
}
}
fclose(storePtr);
}
void updateRegister(FILE *fPtr){
int account;
float transaction;
struct clientData client;
printf("Give the number of the account you want to update (1-100): ");
scanf("%d", &account);
fseek(fPtr, (account-1) * sizeof( struct clientData ), SEEK_SET);
fread(&client, sizeof( struct clientData ), 1, fPtr);
if (client.accountNumb == 0)
printf("Account #%d has no info\n", account);
else {
fprintf("%-6%d-16s%-11s%10.2f\n", client.accountNumb, client.lastName, client.firstName, client.money);
printf("Input Debit(+) or Payment(-):");
scanf("%f",&transaction);
client.money += transaction;
printf("%-6%d-16s%-11s%10.2f\n\n", client.accountNumb, client.lastName, client.firstName, client.money);
fseek(fPtr, (account-1)* sizeof(struct clientData), SEEK_SET);
fwrite(&client, sizeof(struct clientData), 1, fPtr);
}
}
void excludeRegister (FILE *fPtr ){
struct clientData client, nullClient = {0,"","",0};
int accountNumb;
printf("Give the Account Number you want to Exclude (1-100): ");
scanf("%d", &accountNumb);
fseek(fPtr, (accountNumb-1) * sizeof( struct clientData ), SEEK_SET);
fread(&client, sizeof(struct clientData) , 1, fPtr);
if (client.accountNumb == 0)
printf("Account %d doesnt exist\n", accountNumb);
else {
fseek(fPtr,(accountNumb-1) * sizeof( struct clientData), SEEK_SET);
fwrite(&nullClient, sizeof(struct clientData),1, fPtr);
}
}