Introduction

Learn how to perform input and output operations on a file.

Why are files needed?

Often it becomes necessary to store data in a file rather than storing it in memory or displaying it on screen — for example, when we need to store data offline or transfer it from one computer to another, etc. At such times, file IO operations need to be done.

Types of files

There are two types of files:

  • Text files
    Text files are files with the extension .txt. The content of such a file is plain text that can be easily edited. Though these files are easily readable, they are not secure and take more storage space. This is why another file type is needed.
  • Binary files
    Binary files are files with the extension .bin. They store data in binary form instead of plain text. Due to this, they are not easily readable and are hence more secure.

File operations

Four major operations can be performed on either of the above-mentioned files. They are:

  • Creating a new file
  • Opening an existing file
  • Writing to an existing file
  • Closing a file

This chapter presents examples related to file handling in C programming.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.