File Handling in C Gate Notes 92
File Handling in C Gate Notes 92
"File handling in C" helps us create, manipulate, update, and delete files stored on the
local file system using C programs.
Sometimes merely displaying the facts on the console is insufficient. The quantity of
data displayed on the console may be extremely small or very vast. Because the
memory is volatile, it is hard to repeatedly restore the programmatically generated data.
However, we can store data on the local file system, which is volatile and accessible
whenever we need to. Here, file handling in C becomes necessary.
What is a File in C?
A file in C can be defined as a sequence of bytes. A file can be a text file straight away
or a binary code sequence. Regardless of this, it is treated as a sequence of bytes. It
can be a text file, binary file, or any other type of file. A file can contain various types of
data, such as numbers, characters, strings, or images.
Types of Files in C
A file is typically used in computers to store user data. In other words, a computer uses
files to store data. These data files are now available in the C language in 2 different
forms, namely:
• Binary Files
• Text Files
The binary files contain information and data using the 0s and 1s binary coding scheme
(the binary number system). As a result, the files take up far less storage space. In
other words, binary files retain data and information like a computer does in its memory.
As a result, it is easier to access than a text file.
The disadvantage of text files in a program is overcome by the binary files, which are
produced with the extension.bin and can only be read by computers, as opposed to
humans, who cannot read them.
The most fundamental types of files that a user in a C program can produce are text
files. We produce the text files using a basic text editor with the.txt extension. These
files internally hold information in ASCII character format. However, the content or text
appears in a readable format when we view them.
Thus, accessing and using text files is quite simple. There is, however, one significant
drawback: lack of security. Information isn't secure in a text file because it can be
retrieved quickly. Apart from the security issue, text files occupy a larger storage space
than binary files.
In this example, we are trying to open a file that does not exist. The fopen() function
returns a NULL pointer, indicating that the file could not be opened. We then use the
perror() function to print the error message along with the value of errno.In this example,
we are trying to open a file that does not exist. The fopen() function returns a NULL
pointer, indicating that the file could not be opened. We then use the perror() function to
print the error message along with the value of errno.
By handling errors in this way, we can ensure that our program does not crash due to
file operations that fail.
Action Description
Writing to a file Creating a new file or overwriting an existing file and writing data to it.
Appending to a
Opening an existing file and adding data to its end.
file
Closing a file Terminating the connection between the program and the file.
Checking for Ensuring that the file operations are successful and handling errors if they
errors occur.