C File Handling

What are Unions in C?

File Handling in C is the process of creating, opening, reading, writing, updating, and closing files using C programs.

Instead of storing data only in memory, file handling allows data to be stored permanently in files. This makes it possible to save information even after the program is closed.

C provides several built-in functions through the stdio.h library to perform file operations efficiently.

Example

FILE *fp;

fp = fopen(“student.txt”, “w”);

Explanation

• FILE is a predefined data type used for file operations.

• fp is a file pointer.

• fopen() is used to open or create a file.

• “student.txt” is the file name.

• “w” opens the file in write mode.

Why File Handling is Important?

File Handling is important because it:

• Stores data permanently in files.

• Prevents data loss after the program ends.

• Makes it easy to read and update stored information.

• Is widely used in databases, reports, and applications.

• Helps manage large amounts of data efficiently.

Types of File Operations in C

C provides different file operations to manage files efficiently.

The most commonly used file operations are:

• Reading a File

• Writing to a File

• Appending to a File

Each operation is performed using different file modes and functions.

Reading a File

Reading a file means retrieving data stored in a file.

The fopen() function is used with “r” mode to open an existing file for reading.

Example

FILE *fp;

fp = fopen(“student.txt”, “r”);

Explanation

• FILE is the predefined file data type.

• fp is a file pointer.

• fopen() opens the file.

• “r” stands for Read Mode.

• The file must already exist before opening it in read mode.

Writing to a File

Writing to a file means storing new data in a file.

The fopen() function is used with “w” mode to create a new file or overwrite the contents of an existing file.

Example

FILE *fp;

fp = fopen(“student.txt”, “w”);

Explanation

• fopen() opens or creates a file.

• “w” stands for Write Mode.

• If the file does not exist, a new file is created.

• If the file already exists, its previous contents are erased.

• Data can be written using functions like fprintf() and fputs().

Appending to a File

Appending means adding new data to the end of an existing file without deleting the previous contents.

The fopen() function is used with “a” mode for append operations.

Example

FILE *fp;

fp = fopen(“student.txt”, “a”);

Explanation

• “a” stands for Append Mode.

• New data is added at the end of the file.

• Existing data remains unchanged.

• Append mode is useful for logs, reports, and records.

Advantages of File Handling

• Stores data permanently.

• Prevents data loss after program execution.

• Supports reading, writing, and updating files.

• Makes data management easier.

• Widely used in real-world applications such as databases, reports, and record management.

Example Program

Program

#include <stdio.h>

int main()
{
FILE *fp;

fp = fopen(“student.txt”, “w”);

if(fp == NULL)
{
printf(“File could not be opened.”);
return 1;
}

fprintf(fp, “Welcome to SkillovaHub”);

fclose(fp);

printf(“Data written successfully.”);

return 0;
}

Output

Data written successfully.

Explanation

• FILE *fp declares a file pointer.

• fopen() opens or creates the file in write mode.

• The NULL check ensures the file was opened successfully.

• fprintf() writes text to the file.

• fclose() closes the file and saves the changes.

• printf() displays the success message on the screen.

Key Points

• File handling stores data permanently.

• fopen() is used to open or create a file.

• fclose() is used to close a file.

• fprintf() writes formatted data to a file.

• fscanf() reads formatted data from a file.

• File modes such as “r”, “w”, and “a” determine how a file is accessed.

• Always close a file after completing file operations.