A File is an object that stores data, information, settings or commands. These files can be of different types which are generally used for different purposes. C Programming Language allows creating, accessing, reading, writing and other file operations that can control the input and output data.
The data from the File is stored in the disk space available on the system in the form of binary data. The storage of this data varies from OS to OS. Also, we have a list of library functions that enables us to handle all the file operations.
Why do we need Files?
Here are some reasons why we create files:
- Consider the situation where the program handles a large quantity of data. Now the memory is limited to the amount that can be displayed on the screen. To overcome this limitation we make use of Files that allows us to handle a large quantity of data without any complexities.
- The data displayed on the screen is stored in the memory for only a very short period of time. As the program terminates the data is lost. With the help of Files, we can avoid this. The data can be stored for any period of time without losing it i.e. permanent storage.
- Also, Files enable a user to transfer data from one system to another without any ambiguity.
Before moving on to details of files and their operations we shall discuss a bit about the type of Files that we use here for different operations.
Text and Binary are the file types that you should have a basic idea before moving further into the topic.
Text Files –
A text file contains textual information in the form of alphabets, digits and special characters or symbols.
This file can be created using a text editor and can be read and understood by a human being as they are in plain language.
Binary Files –
On the other hand, a binary file contains bytes or a compiled version of a text file i.e. data in the form of 0’s and 1’s. They can hold higher amount of data, are not readable easily and provides a better security than text files.
Here is the list of file operations that can be carried out on a file. We will discuss them in detail in our future tutorials, here in this we will focus on the file types:
- Creation of a new file
- Opening or accessing an existing file
- Reading from a file
- Writing to a file
- Seeking in a file or moving to a specific location
- Closing a file
The file pointer
Before moving on to the file operations it is necessary to understand file pointers. The file pointer points to the structure that contains details of the file like name, the size of the file, position, read/write status etc. The file pointer is declared as follows:
The FILE is the structure which stores the details and has been declared in the header file “stdio.h“(Standard input/output header file).
File Modes for opening a file
Here are some file modes that we shall be using in our code to access the file that is to be operated. Note that all the file modes are strings and must be enclosed in double quotes. The function fopen() is used to open the files. The function is used in the following way:
Now have a look at the modes which are used by the function fopen().
|r||This file mode opens the file for “reading“. If the file doesn’t exist then the function fopen() returns NULL.|
|rb||This file mode opens the file for “reading in binary mode“. If the file doesn’t exist then fopen() returns NULL.|
|w||Opens the file for “writing“. If the file exists the content is overwritten else a new file is created.|
|wb||Opens the file for “writing in binary mode“. If the file exists the content is overwritten else a new file is created.|
|a||Opens the file for “appending“, i.e., writing data at the end of the file. If the file doesn’t exist then a new file is created.|
|ab||Opens the file for “appending in binary mode“. If the file doesn’t exist then a new file is created.|
|r+||Opens the file for both “reading and writing“. If the file doesn’t exist then the function fopen() returns NULL.|
|rb+||Opens the file for both “reading and writing in binary mode“. If the file doesn’t exist then the function fopen() returns NULL.|
|w+||Opens the file for both “reading and writing“. If the file exists the content is overwritten else a new file is created.|
|wb+||Opens the file for both “reading and writing in binary mode“. If the file exists the content is overwritten else a new file is created.|
|a+||Opens the file for both “reading and appending“. If the file doesn’t exist then a new file is created.|
|ab+||Opens the file for both “reading and appending in binary mode“. If the file doesn’t exist then a new file is created.|
Read More – How to create, read and perform other operations on Files in C
So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.
Keep Coding!! Happy Coding!! 🙂