---
title: "Opening and closing a data file"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:open-close-file"
source: https://www.bctengineer.com/q/semester_1/computer_programming/open-close-file
site: BCT Engineering
---

# Opening and closing a data file

Computer Programming · Chapter 10: Data Files

- Asked in: 2070 Chaitra, 4 marks; 2068 Shrawan, 1+3 marks (asked 2 times)
- Video: [#29: C File Handling | [2025] C Programming for Beginners](https://www.youtube.com/watch?v=MQIF-WMUOL8) — Programiz
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/open-close-file
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

What do you mean by opening a data file? How is this accomplished? Define opening and closing a file along with suitable examples.

## Short answer

Opening a file with fopen connects a disk file to a FILE pointer, specifying mode (r, w, a). Always check for NULL. Closing with fclose flushes buffers, releases resources, and prevents data loss.

## Answer

#### Opening a file
Opening a file means connecting a file on the disk to a program through a **file pointer**, so that the program can read from or write to it. The function `fopen()` does this:

`FILE *fp = fopen("filename", "mode");`

It returns the file pointer, or `NULL` if the file cannot be opened (for example, a file for reading does not exist), so the result must always be checked. Common modes: `"r"` read, `"w"` write (creates/erases), `"a"` append.

#### Closing a file
`fclose(fp);` disconnects the file: it writes the remaining buffered data to the disk, releases the file pointer and the resources of the system, and prevents data loss. The number of files that can be open at one time is limited, so files should be closed when work is finished.

```c
#include <stdio.h>

int main(void)
{
    FILE *fp;                              /* file pointer */

    fp = fopen("data.txt", "w");           /* open for writing */
    if (fp == NULL) {                      /* check that opening succeeded */
        printf("Unable to open the file\n");
        return 1;
    }
    fprintf(fp, "Hello file\n");
    fclose(fp);                            /* close: flush buffer, free the file */
    printf("File written and closed\n");
    return 0;
}
```

Output: `File written and closed`

## Check yourself

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

1. What does opening a data file mean?
   A. Deleting its contents
   B. Connecting the disk file to the program through a file pointer
   C. Copying it into memory completely
   D. Printing its name
   Answer: B. fopen() connects the file with a FILE pointer so the program can read or write it. (Key point: What opening means)

2. What does fopen() return when it cannot open the file?
   A. 0 as an int
   B. EOF
   C. NULL
   D. A pointer to an empty file
   Answer: C. It returns NULL, so the result must always be checked. (Key point: fopen failure)

3. What is a key effect of fclose(fp)?
   A. It writes remaining buffered data to disk and releases the file
   B. It erases the file
   C. It reopens the file
   D. It sets the mode to append
   Answer: A. Closing flushes the buffer, releases resources and prevents data loss. (Key point: Purpose of fclose)

4. What does mode "w" do if the file already exists?
   A. Appends at the end
   B. Erases the old contents
   C. Fails
   D. Opens read-only
   Answer: B. "w" creates the file or erases its previous contents. (Key point: Mode w)

## Related in Computer Programming

- [Data file and file modes](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adata-file-modes) — Computer Programming, chapter 10
- [Macro expansion and file inclusion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Amacro-expansion-file-inclusion) — Computer Programming, chapter 3
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [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
- [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
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10

## Related in other subjects

- [Laplace: current with S1 closing and S2 opening at 4 ms](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_circuit_theory%3Alaplace-two-switch-s1-s2-4ms) — Electrical Circuit Theory, chapter 4
- [Selecting support type for underground opening](https://www.bctengineer.com/question.md?key=semester_4%2Fengineering_geology_ii%3Asupport-type-underground) — Engineering Geology II, chapter 6
- [Stream and file stream classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astream-file-classes) — Object Oriented Programming, chapter 8
- [Google File System architecture](https://www.bctengineer.com/question.md?key=semester_8%2Fbig_data_technologies%3Agfs-architecture) — Big Data Technologies, chapter 2, asked 2×
