---
title: "Write student records to STUDENT.DAT"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-student-dat"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-student-dat
site: BCT Engineering
---

# Write student records to STUDENT.DAT

Computer Programming · Chapter 10: Data Files

- Asked in: 2072 Kartik, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-student-dat
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program to write name, roll no and age of five students into a disk file name "STUDENT.DAT".

## Short answer

Method: Open "STUDENT.DAT" in write mode ("w"). Loop 5 times: read name, roll, age; write each record as a text line with fprintf. Result: File contains five lines, each with name, roll number, and age.

## Answer

**Logic.** A text file `STUDENT.DAT` is opened in write mode (the opening is checked for `NULL`). A loop of five repetitions reads name, roll number and age, and `fprintf()` writes them as one line in the file.

```c
#include <stdio.h>

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

    fp = fopen("STUDENT.DAT", "w");
    if (fp == NULL) {
        printf("Cannot open STUDENT.DAT\n");
        return 1;
    }
    for (int i = 1; i <= 5; i++) {
        printf("Student %d (name roll age): ", i);
        scanf("%29s %d %d", name, &roll, &age);
        fprintf(fp, "%s %d %d\n", name, roll, age);
    }
    fclose(fp);
    printf("Records written to STUDENT.DAT\n");
    return 0;
}
```

**Sample run**

Input: `Ram 1 19 Sita 2 18 Hari 3 20 Gita 4 19 Bibek 5 21`

```text
Student 1 (name roll age): Student 2 (name roll age): Student 3 (name roll age): Student 4 (name roll age): Student 5 (name roll age): Records written to STUDENT.DAT
```

STUDENT.DAT afterwards contains the lines:

```text
Ram 1 19
Sita 2 18
Hari 3 20
Gita 4 19
Bibek 5 21
```

## Check yourself

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

1. Which mode creates STUDENT.DAT for writing the records?
   A. "r"
   B. "w"
   C. "rb"
   D. "x"
   Answer: B. Write mode creates the file and allows writing. (Key point: Mode for STUDENT.DAT)

2. Which call writes name, roll and age as one line?
   A. fprintf(fp, "%s %d %d\n", name, roll, age);
   B. scanf(fp, name, roll, age);
   C. fgets(name, 30, fp);
   D. fread(name, 1, 1, fp);
   Answer: A. fprintf writes formatted text to the file. (Key point: Writing a record)

## Related in Computer Programming

- [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
- [Student records with two functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-student-read-display-functions) — Computer Programming, chapter 8
- [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
- [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
- [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

## Related in other subjects

- [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
- [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 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
- [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
