C – File System (I/O)5 min read

In this tutorial, you will learn about file handling in C. You will learn how to read from a file, write to a file. close a file and more.

File pointers: It is not enough to just display the data on the screen. We need to save it because memory is volatile and its contents would be lost once the program terminated, so if we need some data again there are two ways, one is retyped via keyboard to assign it to a particular variable, and the other is regenerate it via programmatically both options are tedious.

At such times it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a “file” on the disk.

Introduction to file

Until now we have been using the functions such as scanf, printf, getch, putch, etc to read and write data on the variables and arrays for storing data inside the programs But this approach poses the following problems.

  • The data is lost when program terminated or variable goes out of scope.
  • Difficulty to use large volume of data.

We can overcome these problems by storing data on secondary devices such as Hard Disk. The data is stored on the devices using the concept of “file”. A file is a collection of related records, a record is composed of several fields and a field is a group of characters. The most straightforward use of files is via a file pointer.

We need to declare the pointer first in a program and is done in the following ways:

The type FILE is not a basic type, instead, it is defined in the header file stdio.h, this file must be included in your program


Types of Files

Basically there are two types of file:

  1. Text files: These files are .txt files that contain the plain text when you open it and easy to edit and delete. You can create such files with text editors such as Notepad.
  2. Binary file: These are the .bin files present in the computer. They store the data in a binary form such as in 0’s and 1s instead of plain text. Although it is harder to understand, it gives better security than plain text files.

File Operation

  • Create a new file (fopen with attributes as “a” or “a+” or “w” or “w++”).
  • Open an existing file (fopen).
  • Read from file (fscanf or fgets).
  • Write to a file (fprintf or fputs).
  • Moving a specific location in a file(Seeking) (fseek, rewind).
  • Closing File (fclose).

Opening a file:

fopen() is used to open a file by giving two values such as file_name and mode to which you want to open the file. The various modes are stated below.

There are various modes to open a file. Check the table given below.

File ModesDescription
rread
wwrite, overwrite file if it exists
awrite, but append instead of overwrite
w+read & write, do not destroy file if it exists
a+read & write, but append instead of overwrite
b+may be appended to any of the above to force the file to be opened in binary mode rather than text mode.

Sequential file access is performed with the following library functions: 

  • fopen()  – Create a new file
  • fclose()  – Close file
  • getc()    – Read character from file
  • putc()    – Write a character to a file
  • getw()   – Read Integer from file
  • putw()   – Write Integer to a file
  • fprintf()  – Write set of data values
  • fscanf()  – read set of data values

Closing a file:

fclose() function is used to close the file once the operation on file is done. And it is done in a flowing way:

Example on how to use open and close mode.


Reading from a file:

Reading operation are performed fgetc(), fgets() and fscanf(). You can use them according to your choice. If you want to read character by character then you may use fgetc() or for line by line, you can use fgets().

The end of the file is denoted by EOF(end of file) ot NULL character (\0) value at the end. It tells the function that it has reached the end of the file.


Writing to a file:

Writing operation in a file can be performed with the use of fprintf, fputc and fputs.

  • fputc(char, file_pointer): It writes a character to the file.
  • fputs(str, file_pointer): It writes a string to the file..
  • fprintf(file_pointer, str, variable_lists): The first value is the file_pointer that points to the String that needs to be printed.

Example of Writing and closing a file in C programming.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …