Detailed File Handling in C Language

In programming, we may need to generate specific input data multiple times. Sometimes, simply displaying data on the console is not enough. The data to be displayed may be quite large, while the console can only display a limited amount of data, and due to the volatile nature of memory, it cannot repeatedly fetch programmatically generated data. However, if we need to do this, we can store it on the volatile local file system, where it can be accessed each time. This is where file handling in C language comes into play.

File handling in C allows us to create, update, read, and delete files on the local file system through C programs. The following operations can be performed on files:

  • Create a new file

  • Open an existing file

  • Read data from a file

  • Write data to a file

  • Delete a file

Functions for File Handling

There are many functions in the C library for opening, reading, writing, searching, and closing files. The list of file functions is as follows:

Index Function Description
1 fopen() Open a new or existing file
2 fprintf() Write data to a file
3 fscanf() Read data from a file
4 fputc() Write a character to a file
5 fgetc() Read a character from a file
6 fclose() Close a file
7 fseek() Set the file pointer to a given position
8 fputw() Write an integer to a file
9 fgetw() Read an integer from a file
10 ftell() Return the current position
11 rewind() Set the file pointer to the beginning of the file

Opening a File: fopen()

Before we can read, write, or update a file, we must open it. The fopen() function is used to open a file. The syntax for the fopen() function is as follows:

FILE fopen( const char *filename, const char *mode );

The fopen() function takes two parameters:

  • Filename (string). If the file is stored at a specific location, the path must be included. For example, the filename can be “c://some_folder/some_file.ext”.

  • The mode for opening the file. It is a string.

👇 Click to Claim 👇
👉 C Language Knowledge Resource Collection

We can use one of the following modes in the fopen() function.

Mode Description
r Open a text file in read mode
w Open a text file in write mode
a Open a text file in append mode
r+ Open a text file in read/write mode
w+ Open a text file in read/write mode
a+ Open a text file in read/write mode
rb Open a binary file in read mode
wb Open a binary file in write mode
ab Open a binary file in append mode
rb+ Open a binary file in read/write mode
wb+ Open a binary file in read/write mode
ab+ Open a binary file in read/write mode

The fopen function works as follows.

  • First, it searches for the file to be opened.

  • Then, it loads the file from the disk into the buffer. The buffer is used to improve the efficiency of read operations.

  • It sets a character pointer that points to the first character of the file.

Consider the following example, which opens a file in write mode.

#include <stdio.h>
void main() {  FILE *fp;  char ch;     fp = fopen("file_handle.c", "r");  while (1) {      ch = fgetc(fp);      if (ch == EOF)          break;      printf("%c", ch);  }     fclose(fp);}

Output

This will print the contents of the file.

#include <stdio.h>
void main() {  FILE *fp; // file pointer
char ch;  fp = fopen("file_handle.c", "r");  while (1) {      ch = fgetc(fp); // Each character of the file is read and stored in the character variable.
if (ch == EOF) break;      printf("%c", ch);  }  fclose(fp);}

Closing a File: fclose()

The fclose() function is used to close a file. After performing all operations on a file, it is necessary to close it. The syntax for the fclose() function is as follows:

int fclose(FILE *fp);

Detailed File Handling in C Language


Popular Recommendations
  • C Language Tutorial – Detailed Explanation of Nested Structures in C Language

  • C Language Tutorial – Detailed Explanation of Structure Padding in C Language

  • C Language Algorithm – “Parentheses Generation” Algorithm Problem

Leave a Comment