Skip to main content

Random access file transaction processing

Short answer

Create a random-access file of 100 fixed-size Account records. Add records by writing at position (accno-1)*sizeof(Account). Update by reading, modifying balance, seeking back and writting. Delete by writing a blank record with accno 0. Display only records with non-zero accno.

Write a program for transaction processing that write and read object randomly to and from a random access file so that user can add, update, delete and display the account information (accountnumber, lastname, firstname, totalbalance).

A random access file stores fixed-size records, so record n is found at byte position (n-1) × sizeof(record). The account number is used as the record number. Deleted accounts are marked by account number 0.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

struct Account {
    int  accno;            // 0 means empty / deleted
    char last[15], first[15];
    float balance;
};

const int MAX = 100;
fstream f;

void init() {                                 // create empty file
    f.open("acc.dat", ios::in | ios::binary);
    if (f) { f.close(); return; }
    Account blank = {0, "", "", 0};
    f.open("acc.dat", ios::out | ios::binary);
    for (int i = 0; i < MAX; i++) f.write((char*)&blank, sizeof(blank));
    f.close();
}

void add() {
    Account a;
    cout << "Account no (1-100), last, first, balance: ";
    cin >> a.accno >> a.last >> a.first >> a.balance;
    f.seekp((a.accno - 1) * sizeof(Account));
    f.write((char*)&a, sizeof(a));
}

void update() {
    int n; float amt; Account a;
    cout << "Account no and amount to add (-ve to withdraw): ";
    cin >> n >> amt;
    f.seekg((n - 1) * sizeof(Account));
    f.read((char*)&a, sizeof(a));
    if (a.accno == 0) { cout << "No such account\n"; return; }
    a.balance += amt;
    f.seekp((n - 1) * sizeof(Account));
    f.write((char*)&a, sizeof(a));
}

void del() {
    int n; cout << "Account no to delete: "; cin >> n;
    Account blank = {0, "", "", 0};
    f.seekp((n - 1) * sizeof(Account));
    f.write((char*)&blank, sizeof(blank));
}

void display() {
    Account a;
    f.seekg(0);
    while (f.read((char*)&a, sizeof(a)))
        if (a.accno != 0)
            cout << a.accno << "\t" << a.last << "\t"
                 << a.first << "\t" << a.balance << endl;
    f.clear();
}

int main() {
    init();
    f.open("acc.dat", ios::in | ios::out | ios::binary);
    int ch;
    do {
        cout << "\n1.Add 2.Update 3.Delete 4.Display 5.Exit: ";
        cin >> ch;
        switch (ch) {
            case 1: add(); break;
            case 2: update(); break;
            case 3: del(); break;
            case 4: display(); break;
        }
    } while (ch != 5);
    f.close();
    return 0;
}

seekp() moves the put pointer for writing and seekg() moves the get pointer for reading, giving direct access to any account without scanning the file.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Programming chapter 8 questions →