---
title: "Random access file transaction processing"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:random-access-transaction"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/random-access-transaction
site: BCT Engineering
---

# Random access file transaction processing

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2072 Kartik, 7 marks
- Video: [Example program: Transaction Processing using Random Access File](https://www.youtube.com/watch?v=G2DLPbAt2kg) — Saraswathi Arumainathan
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/random-access-transaction
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

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).

## 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.

## Answer

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.

```cpp
#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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In the random access file, where is account number n stored?
   A. At byte n + sizeof(record)
   B. At the end of the file
   C. At byte (n-1) x sizeof(record)
   D. At byte n / sizeof(record)
   Answer: C. Records are fixed size, so record n starts at (n-1) times the record size; the account number is the record number. (Key point: Record position formula)

2. Which statement about seekp() and seekg() is correct?
   A. seekp moves the get pointer, seekg moves the put pointer
   B. seekp moves the put pointer for writing, seekg moves the get pointer for reading
   C. Both only work with text files
   D. They are used only to close the file
   Answer: B. seekp positions the put (write) pointer and seekg the get (read) pointer, giving direct access without scanning. (Key point: seekp vs seekg)

3. How does the program delete an account?
   A. Removes bytes from the file and shifts later records
   B. Overwrites that record with a blank record whose accno is 0
   C. Calls remove() on the file
   D. Sets the balance to a negative number
   Answer: B. Random access files have fixed-size slots, so a deleted record is marked by writing accno 0 (empty). (Key point: Deleting an account)

4. What are the steps in update()?
   A. Write directly at the position without reading
   B. Seek, read the record, change the balance, seek back and write it
   C. Delete the record and add it again at the end
   D. Read the whole file into memory and rewrite it
   Answer: B. It reads the existing record at its position, adds the amount to the balance, then seeks back and writes it over the old one. (Key point: Update procedure)

## Related in Object Oriented Programming

- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Student records file with CRN search](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-crn-file-search) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [File I/O operations, errors, random access](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-errors-random-access) — Computer Programming, chapter 10
- [TSI in random read, sequential write mode](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Atsi-random-read-sequential-write) — Telecommunications, chapter 4, asked 2×
- [TSI in sequential read, random write mode](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3Atsi-sequential-read-random-write) — Telecommunications, chapter 4, asked 2×
- [Random scan versus raster scan display](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_graphics%3Arandom-vs-raster-display) — Computer Graphics, chapter 1, asked 7×
