---
title: "Student file read/write and modify by roll"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:student-file-modify-roll"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-file-modify-roll
site: BCT Engineering
---

# Student file read/write and modify by roll

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

- Asked in: 2073 Shrawan, 7 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-file-modify-roll
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Write a program to read and write the information of 10 students in a file. Also modify the student information according to the given roll number.

## Short answer

Write 10 student records to binary file student.dat. To modify by roll number, open in read/write mode, scan records with read(). On match, use seekp to move back one record and overwrite with new data.

## Answer

The program (1) writes 10 student records to a binary file, (2) displays them, and (3) modifies a record chosen by roll number using `seekp()`.

```cpp
#include <iostream>
#include <fstream>
using namespace std;

class Student {
    int roll;
    char name[30];
    float marks;
public:
    void getData() {
        cout << "Roll, Name, Marks: ";
        cin >> roll >> name >> marks;
    }
    void display() { cout << roll << "\t" << name << "\t" << marks << endl; }
    int getRoll() { return roll; }
};

int main() {
    Student s;
    fstream f("student.dat", ios::out | ios::binary);
    for (int i = 0; i < 10; i++) {            // write 10 students
        s.getData();
        f.write((char*)&s, sizeof(s));
    }
    f.close();

    f.open("student.dat", ios::in | ios::binary);   // read and show
    while (f.read((char*)&s, sizeof(s)))
        s.display();
    f.close();

    int r;
    bool found = false;
    cout << "Enter roll number to modify: ";
    cin >> r;
    f.open("student.dat", ios::in | ios::out | ios::binary);
    while (f.read((char*)&s, sizeof(s))) {
        if (s.getRoll() == r) {
            cout << "Enter new data:\n";
            s.getData();
            f.seekp(-(long)sizeof(s), ios::cur);   // back one record
            f.write((char*)&s, sizeof(s));
            found = true;
            break;
        }
    }
    f.close();
    if (!found) cout << "Roll number not found\n";

    f.open("student.dat", ios::in | ios::binary);   // show updated file
    while (f.read((char*)&s, sizeof(s)))
        s.display();
    f.close();
    return 0;
}
```

## Check yourself

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

1. Which mode is used to read and update records in place?
   A. ios::in only
   B. ios::app
   C. ios::out only
   D. ios::in | ios::out | ios::binary
   Answer: D. Both reading and writing are needed, and out without in would truncate. (Key point: File mode for modify)

2. Why call f.seekp(-(long)sizeof(s), ios::cur) after finding the roll?
   A. The read moved past the record, so go back one record before overwriting
   B. To move to end of file
   C. To delete the record
   D. To rewind to file start
   Answer: A. After read, the put position is just after the record; seeking back one record size overwrites it. (Key point: Moving back)

3. How is the record to modify identified?
   A. By comparing getRoll() with the entered roll number
   B. By file name
   C. By record order only
   D. By name
   Answer: A. Records are read sequentially and getRoll() is compared with the input. (Key point: Finding record)

4. What happens if the roll number is absent?
   A. found stays false and Roll number not found is shown
   B. The program crashes
   C. The file is erased
   D. The first record is changed
   Answer: A. found is only set true on a match, so the not-found message prints. (Key point: Not found)

## Related in Object Oriented Programming

- [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
- [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
- [Student file displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — 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
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [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

## Related in other subjects

- [Binary file of 48 students; highest mark](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-binary-48-students-highest) — Computer Programming, chapter 10
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [Store 48 students in file with append](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-48-students-append) — Computer Programming, chapter 10
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
