C Programming • Pointers & File handling
C Programming / File handling: different modes of opening a file in C

File handling: different modes of opening a file in C

Notes 5 Pointers & File handling

File handling in C is used to store data permanently in a file and to read or write that data whenever required. C provides file-handling functions through the stdio.h library.

Notes

File handling in C is used to store data permanently in a file and to read or write that data whenever required. C provides file-handling functions through the stdio.h library.

💡 Example
#include <stdio.h> FILE *fp;

Here, fp is a file pointer that is used to refer to a file opened by the program.

File Pointer

A file pointer is a pointer of type FILE used by C's file-handling functions to keep track of an opened file. It is declared using FILE * and is passed to functions such as fopen() and fclose().

💡 Example
FILE *fp;

fp will be used to refer to the file opened by the program.

fopen() Function

The fopen() function is used to open a file. It takes the file name and the required opening mode as arguments and returns a file pointer when the file is opened successfully. If the file cannot be opened, it returns NULL.

💡 Example
FILE *fp; fp = fopen("data.txt", "r");

The file data.txt is opened in read mode.

Syntax of fopen()

FILE *pointer_name; pointer_name = fopen("file_name", "mode");
💡 Example
FILE *fp; fp = fopen("student.txt", "r");

Here, fp refers to student.txt opened in read mode.

Checking Whether a File Opened Successfully

A program should check the return value of fopen(). If the returned pointer is NULL, the file could not be opened successfully.

💡 Example
fp = fopen("data.txt", "r"); if (fp == NULL) { printf("Cannot open file"); }

The condition checks whether the file-opening operation failed.

File Opening Modes

The second argument of fopen() specifies how the program wants to use the file. Different modes determine whether the file will be read, written, appended, or opened for both reading and writing.

1. "r" — Read Mode

The r mode opens an existing file for reading. The file must already exist. If the file cannot be opened, fopen() returns NULL.

💡 Example
fp = fopen("data.txt", "r");

The program can read the existing contents of data.txt.

2. "w" — Write Mode

The w mode opens a file for writing. If the file already exists, its previous contents are overwritten. If the file does not exist, a new file is created.

💡 Example
fp = fopen("data.txt", "w");

The program can write new contents to the file. Existing contents, if any, are discarded when the file is opened this way.

3. "a" — Append Mode

The a mode opens a file for adding new content at the end. Existing contents are preserved. If the file does not exist, a new file is created.

💡 Example
fp = fopen("data.txt", "a");

New data can be added without removing the existing contents.

4. "r+" — Read and Write Mode

The r+ mode opens an existing file for both reading and writing. The file must already exist; it does not create a new file if the file is absent.

💡 Example
fp = fopen("data.txt", "r+");

The program can read existing contents and also write to the file.

5. "w+" — Read and Write Mode

The w+ mode opens a file for both reading and writing. If the file exists, its previous contents are overwritten. If it does not exist, a new file is created.

💡 Example
fp = fopen("data.txt", "w+");

The program can write new contents and read them back, but the old contents are lost when an existing file is opened in this mode.

6. "a+" — Append and Read Mode

The a+ mode opens a file for reading and for adding new contents at the end. Existing contents are preserved. If the file does not exist, a new file is created.

💡 Example
fp = fopen("data.txt", "a+");

Existing contents can be read and new contents can be added at the end of the file.

Comparison of File Opening Modes

Mode Purpose If File Does Not Exist Existing Contents
r Read Cannot open Preserved
w Write Creates file Overwritten
a Append Creates file Preserved
r+ Read + Write Cannot open Preserved
w+ Read + Write Creates file Overwritten
a+ Read + Append Creates file Preserved

fclose() Function

After finishing the file operation, the file should be closed using fclose(). It takes the file pointer as its argument and closes the corresponding opened file.

💡 Example
fclose(fp);

Here, fp is the file pointer used to refer to the file that needs to be closed.

Basic File Handling Flow

Declare FILE pointer ↓ Open file using fopen() ↓ Check for NULL ↓ Read / Write / Append ↓ Close file using fclose()

Practical Example

Problem Statement

Write a C program to create a file, write a message into it, close the file, and display a confirmation message.

Learning Outcomes

  • Declare a file pointer.
  • Open a file using fopen().
  • Use write mode to create and write to a file.
  • Close a file using fclose().

Hint

Open student.txt in "w" mode, write a short message, check whether the file was opened successfully, and close it after writing.

Theory

The "w" mode opens a file for writing. If the file does not exist, it is created. If it already exists, its previous contents are overwritten.

Program

#include <stdio.h> int main() { FILE *fp; // open the file in write mode fp = fopen("student.txt", "w"); // check whether the file was opened successfully if (fp == NULL) { printf("Cannot open file "); return 1; } // write data into the file fprintf(fp, "BCA Study Portal - C Programming"); // close the file fclose(fp); printf("Data written successfully "); return 0; }

Expected Output

Data written successfully

File Content

BCA Study Portal - C Programming

Note

The program uses fopen() to create/open the file, fprintf() to write data, and fclose() to close the file. Always check whether fopen() returned NULL before using the file pointer.

Important Exam Differences

📌 Remember

r → Read existing file
w → Write; old contents are overwritten
a → Add data at the end
r+ → Read + Write existing file
w+ → Read + Write; old contents are overwritten
a+ → Read + Append; existing contents are preserved

Common Mistakes

⚠️ Mistake 1

Forgetting to check fp == NULL can lead to using an invalid file pointer.

⚠️ Mistake 2

Using "w" when you want to preserve existing contents can accidentally overwrite the file.

⚠️ Mistake 3

Forgetting fclose() after completing the file operation is bad file-handling practice.

Quick Revision

Function / Mode Remember
FILE * Declares a file pointer.
fopen() Opens a file using a specified mode.
r Read existing file.
w Write; existing contents are overwritten.
a Append at the end.
r+ Read and write an existing file.
w+ Read and write; old contents are overwritten.
a+ Read and append.
fclose() Closes the opened file.

Important Exam Questions

Short Answer Questions

  1. What is file handling in C?
  2. What is a file pointer?
  3. What is the purpose of fopen()?
  4. What happens when fopen() returns NULL?
  5. What is the difference between "r" and "w"?
  6. What is the use of append mode "a"?
  7. Differentiate between "r+" and "w+".
  8. What is the purpose of fclose()?

Long Answer Questions

  1. Explain different modes of opening a file in C with suitable examples.
  2. Explain the use of fopen() and fclose() with a suitable program.
  3. Differentiate between r, w, a, r+, w+ and a+ modes.
  4. Write a C program to create a file and write data into it.
🎥 Recommended Learning

Watch a beginner-friendly explanation of file handling and file opening modes in C.

▶ Watch: File Handling in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for file-opening modes will be provided here.

🧠 Mind Map

Use the mind map for FILE pointer, fopen(), file modes, NULL checking and fclose().