#include struct clientData {     unsigned int acctNum;      char lastName[15];         char firstName[10];        double balance;        }; unsigned int enterChoice(void); void textFile(FILE* readPtr); void updateRecord(FILE* fPtr); void newRecord(FILE* fPtr); void deleteRecord(FILE* fPtr); int main(void) {     FILE* cfPtr;           if ((cfPtr = fopen("accounts.dat", "rb+")) == NULL) {         puts("File could not be opened.");     }     else {         unsigned int choice;                   while ((choice = enterChoice()) != 5) {             switch (choice) {                              case 1:                 textFile(cfPtr);                 break;                              case 2:                 updateRecord(cfPtr);                 break;                              case 3:                 newRecord(cfPtr);                 break;                              case 4:                 deleteRecord(cfPtr);                 break;                              default:                 puts("Incorrect choice");                 break;             }         }         fclose(cfPtr);      } } void textFile(FILE* readPtr) {     FILE* writePtr;           if ((writePtr = fopen("accounts.txt", "w")) == NULL) {         puts("File could not be opened.");     }     else {         rewind(readPtr);          fprintf(writePtr, "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First Name",             "Balance");                  while (!feof(readPtr)) {                          struct clientData client = { 0, "", "", 0.0 };             int result = fread(&client, sizeof(struct clientData), 1, readPtr);                          if (result != 0 && client.acctNum != 0) {                 fprintf(writePtr, "%-6d%-16s%-11s%10.2f\n", client.acctNum,                     client.lastName, client.firstName, client.balance);             }         }         fclose(writePtr);      } } void updateRecord(FILE* fPtr) {         printf("%s", "Enter account to update (1 - 100): ");     unsigned int account;      scanf("%d", &account);        fseek(fPtr, (account - 1) * sizeof(struct clientData), SEEK_SET);          struct clientData client = { 0, "", "", 0.0 };          fread(&client, sizeof(struct clientData), 1, fPtr);     if (client.acctNum == 0) {         printf("Account #%d has no information.\n", account);     }     else {          printf("%-6d%-16s%-11s%10.2f\n\n", client.acctNum, client.lastName,             client.firstName, client.balance);         =         printf("%s", "Enter charge (+) or payment (-): ");         double transaction;          scanf("%lf", &transaction);         client.balance += transaction;          printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName,             client.firstName, client.balance);                  fseek(fPtr, (account - 1) * sizeof(struct clientData), SEEK_SET);                  fwrite(&client, sizeof(struct clientData), 1, fPtr);     } } void deleteRecord(FILE* fPtr) {          printf("%s", "Enter account number to delete (1 - 100): ");     unsigned int accountNum;      scanf("%d", &accountNum);          fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);     struct clientData client;           fread(&client, sizeof(struct clientData), 1, fPtr);     if (client.acctNum == 0) {         printf("Account %d does not exist.\n", accountNum);     }     else {          fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);         struct clientData blankClient = { 0, "", "", 0 };                   fwrite(&blankClient, sizeof(struct clientData), 1, fPtr);     } } void newRecord(FILE* fPtr) {     printf("%s", "Enter new account number (1 - 100): ");     unsigned int accountNum;     scanf("%d", &accountNum);     fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);     struct clientData client = { 0, "", "", 0.0 };     fread(&client, sizeof(struct clientData), 1, fPtr);     if (client.acctNum != 0) {         printf("Account #%d already contains information.\n? ", client.acctNum);         scanf("%14s%9s%lf", &client.lastName, &client.firstName, &client.balance);         client.acctNum = accountNum;         fseek(fPtr, (client.acctNum - 1) * sizeof(struct clientData), SEEK_SET);                fwrite(&client, sizeof(struct clientData), 1, fPtr);     } } unsigned int enterChoice(void) {     printf("%s", "\nEnter your choice\n"         "1 - store a formatted text file of accounts called\n"         "   \"accounts.txt\" for printing\n"         "2 - update an account\n"         "3 - add a new account\n"         "4 - delete an account\n"         "5 - end program\n? ");     unsigned int menuChoice;       scanf("%u", &menuChoice);      return menuChoice; }   Can someone explain this to me. This is C Programming

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#include <stdio.h>


struct clientData {
    unsigned int acctNum; 
    char lastName[15];    
    char firstName[10];   
    double balance;       
};

unsigned int enterChoice(void);
void textFile(FILE* readPtr);
void updateRecord(FILE* fPtr);
void newRecord(FILE* fPtr);
void deleteRecord(FILE* fPtr);

int main(void) {
    FILE* cfPtr; 
    
    if ((cfPtr = fopen("accounts.dat", "rb+")) == NULL) {
        puts("File could not be opened.");
    }
    else {
        unsigned int choice; 
        
        while ((choice = enterChoice()) != 5) {
            switch (choice) {
                
            case 1:
                textFile(cfPtr);
                break;

                
            case 2:
                updateRecord(cfPtr);
                break;
                
            case 3:
                newRecord(cfPtr);
                break;
                
            case 4:
                deleteRecord(cfPtr);
                break;
                
            default:
                puts("Incorrect choice");
                break;
            }
        }
        fclose(cfPtr); 
    }
}

void textFile(FILE* readPtr) {
    FILE* writePtr; 
    
    if ((writePtr = fopen("accounts.txt", "w")) == NULL) {
        puts("File could not be opened.");
    }
    else {
        rewind(readPtr); 
        fprintf(writePtr, "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First Name",
            "Balance");
        
        while (!feof(readPtr)) {
            
            struct clientData client = { 0, "", "", 0.0 };
            int result = fread(&client, sizeof(struct clientData), 1, readPtr);
            
            if (result != 0 && client.acctNum != 0) {
                fprintf(writePtr, "%-6d%-16s%-11s%10.2f\n", client.acctNum,
                    client.lastName, client.firstName, client.balance);
            }
        }

        fclose(writePtr); 
    }
}


void updateRecord(FILE* fPtr) {
   
    printf("%s", "Enter account to update (1 - 100): ");
    unsigned int account; 
    scanf("%d", &account);

  
    fseek(fPtr, (account - 1) * sizeof(struct clientData), SEEK_SET);
    
    struct clientData client = { 0, "", "", 0.0 };

    
    fread(&client, sizeof(struct clientData), 1, fPtr);
    if (client.acctNum == 0) {
        printf("Account #%d has no information.\n", account);
    }
    else { 
        printf("%-6d%-16s%-11s%10.2f\n\n", client.acctNum, client.lastName,
            client.firstName, client.balance);

        =
        printf("%s", "Enter charge (+) or payment (-): ");
        double transaction; 
        scanf("%lf", &transaction);
        client.balance += transaction; 

        printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName,
            client.firstName, client.balance);

        
        fseek(fPtr, (account - 1) * sizeof(struct clientData), SEEK_SET);

        
        fwrite(&client, sizeof(struct clientData), 1, fPtr);
    }
}
void deleteRecord(FILE* fPtr) {
    
    printf("%s", "Enter account number to delete (1 - 100): ");
    unsigned int accountNum; 
    scanf("%d", &accountNum);

    
    fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);

    struct clientData client; 
    

    fread(&client, sizeof(struct clientData), 1, fPtr);

    if (client.acctNum == 0) {
        printf("Account %d does not exist.\n", accountNum);

    }
    else { 
        fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);

        struct clientData blankClient = { 0, "", "", 0 }; 
        
        fwrite(&blankClient, sizeof(struct clientData), 1, fPtr);
    }
}


void newRecord(FILE* fPtr) {
    printf("%s", "Enter new account number (1 - 100): ");
    unsigned int accountNum;
    scanf("%d", &accountNum);

    fseek(fPtr, (accountNum - 1) * sizeof(struct clientData), SEEK_SET);

    struct clientData client = { 0, "", "", 0.0 };

    fread(&client, sizeof(struct clientData), 1, fPtr);
    if (client.acctNum != 0) {
        printf("Account #%d already contains information.\n? ", client.acctNum);
        scanf("%14s%9s%lf", &client.lastName, &client.firstName, &client.balance);

        client.acctNum = accountNum;

        fseek(fPtr, (client.acctNum - 1) * sizeof(struct clientData), SEEK_SET);
      
        fwrite(&client, sizeof(struct clientData), 1, fPtr);
    }
}

unsigned int enterChoice(void) {
    printf("%s", "\nEnter your choice\n"
        "1 - store a formatted text file of accounts called\n"
        "   \"accounts.txt\" for printing\n"
        "2 - update an account\n"
        "3 - add a new account\n"
        "4 - delete an account\n"
        "5 - end program\n? ");

    unsigned int menuChoice;  
    scanf("%u", &menuChoice); 
    return menuChoice;
}

 

Can someone explain this to me. This is C Programming

 

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education