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

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More