---
title: "Store 48 students in file with append"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-48-students-append"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-48-students-append
site: BCT Engineering
---

# Store 48 students in file with append

Computer Programming · Chapter 10: Data Files

- Asked in: 2073 Shrawan, 6 marks
- Video: [C_123 Append Mode in File Handling | C Programming Language](https://www.youtube.com/watch?v=IriDVVG-Az4) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-48-students-append
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program to read name and roll number of 48 students from user and store them in file. If the file already contains data, your program should add new data at the end of the file.

## Short answer

Open file in append mode "a" (creates if missing). Loop 48 times: read name and roll number; write with fprintf. Result: New records are added at the end without deleting existing content.

## Answer

**Key point.** The file is opened in **append mode `"a"`**. If the file already has data, new data is written after the existing data (the old data is kept); if the file does not exist it is created. Mode `"w"` would erase the old content.

```c
#include <stdio.h>
#ifndef N
#define N 48
#endif

int main(void)
{
    FILE *fp;
    char name[30];
    int roll;

    fp = fopen("students.txt", "a");     /* "a": append, creates file if absent */
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    for (int i = 1; i <= N; i++) {
        printf("Student %d (name roll): ", i);
        scanf("%29s %d", name, &roll);
        fprintf(fp, "%s %d\n", name, roll);
    }
    fclose(fp);
    printf("%d records added\n", N);
    return 0;
}
```

**Sample run** (N reduced to 3 for the test with `-DN=3`; the file already had the line "Old 100")

Input: `Ram 1 Sita 2 Hari 3`

```text
Student 1 (name roll): Student 2 (name roll): Student 3 (name roll): 3 records added
```

The file contents afterwards: `Old 100`, `Ram 1`, `Sita 2`, `Hari 3`. With the real N = 48 the loop simply runs 48 times.

## Check yourself

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

1. Which mode keeps existing data and adds new data at the end?
   A. "w"
   B. "r"
   C. "a"
   D. "rb"
   Answer: C. Append mode writes after existing data and creates the file if absent. (Key point: Mode to keep old data)

2. What would happen if "w" were used?
   A. Data would be appended
   B. Old content would be erased
   C. Nothing would be written
   D. The file would be read only
   Answer: B. "w" erases the old contents. (Key point: Mode w danger)

3. In append mode, what happens if the file does not exist?
   A. fopen returns NULL
   B. It is created
   C. The program stops
   D. It opens read-only
   Answer: B. "a" creates the file if absent. (Key point: File absent)

## Related in Computer Programming

- [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
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [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
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10

## Related in other subjects

- [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
- [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
- [Muster roll for user's committee project](https://www.bctengineer.com/question.md?key=semester_8%2Fconstruction_management%3Amuster-roll-user-committee) — Construction Management, 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
