0% found this document useful (0 votes)
4 views

Arrays

File handling in C refers to the methods used to store and manipulate data in files, allowing for permanent storage and retrieval of information. There are two main types of files in C: text files, which are human-readable but less secure, and binary files, which are more compact and secure. Various functions are available for file operations, such as opening, reading, writing, and closing files.

Uploaded by

alekarsamarth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Arrays

File handling in C refers to the methods used to store and manipulate data in files, allowing for permanent storage and retrieval of information. There are two main types of files in C: text files, which are human-readable but less secure, and binary files, which are more compact and secure. Various functions are available for file operations, such as opening, reading, writing, and closing files.

Uploaded by

alekarsamarth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is File Handling in C?

File handling refers to the method of storing data in the C program in the form of an
output or input that might have been generated while running a C program in a data
file, i.e., a binary file or a text file for future analysis and reference in that very
program.

What is a File in C?
A file refers to a source in which a program stores the information/data in the form of
bytes of sequence on a disk (permanently). The content available on a file isn’t
volatile like the compiler memory in C. But the program can perform various
operations, such as creating, opening, reading a file, or even manipulating the data
present inside the file. This process is known as file handling in C.

Why Do We Need File Handling in C?


There are times when the output generated out of a program after its compilation
and running do not serve our intended purpose. In such cases, we might want to
check the program’s output various times. Now, compiling and running the very
same program multiple times becomes a tedious task for any programmer. It is
exactly where file handling becomes useful.
Let us look at a few reasons why file handling makes programming easier for all:

 Reusability: File handling allows us to preserve the information/data


generated after we run the program.
 Saves Time: Some programs might require a large amount of input from
their users. In such cases, file handling allows you to easily access a part of a
code using individual commands.
 Commendable storage capacity: When storing data in files, you can leave
behind the worry of storing all the info in bulk in any program.
 Portability: The contents available in any file can be transferred to another
one without any data loss in the computer system. This saves a lot of effort
and minimises the risk of flawed coding.

Types of Files in a C Program


When referring to file handling, we refer to files in the form of data files. Now, these
data files are available in 2 distinct forms in the C language, namely:

 Text Files
 Binary Files

Text Files
The text files are the most basic/simplest types of files that a user can create in a C
program. We create the text files using an extension .txt with the help of a simple
text editor. In general, we can use notepads for the creation of .txt files. These files
store info internally in ASCII character format, but when we open these files, the
content/text opens in a human-readable form.
Text files are, thus, very easy to access as well as use. But there’s one major
disadvantage; it lacks security. Since a .txt file can be accessed easily, information
isn’t very secure in it. Added to this, text files consume a very large space in storage.
To solve these problems, we have a different type of file in C programs, known as
binary files.

Binary Files
The binary files store info and data in the binary format of 0’s and 1’s (the binary
number system). Thus, the files occupy comparatively lesser space in the storage. In
simpler words, the binary files store data and info the same way a computer holds
the info in its memory. Thus, it can be accessed very easily as compared to a text
file.
The binary files are created with the extension .bin in a program, and it overcomes
the drawback of the text files in a program since humans can’t read it; only
machines can. Thus, the information becomes much more secure. Thus, binary files
are safest in terms of storing data files in a C program.

Operators/Functions that We Use for File Handling in C


We can use a variety of functions in order to open a file, read it, write more data,
create a new file, close or delete a file, search for a file, etc. These are known as file
handling operators in C.
Here’s a list of functions that allow you to do so:

Description of Function Function in Use

used to open an existing file or a new file fopen()

writing data into an available file fprintf()

reading the data available in a file fscanf()

writing any character into the program file fputc()

reading the character from an available file fgetc()

used to close the program file fclose()

used to set the file pointer to the intended file position fseek()

writing an integer into an available file fputw()


used to read an integer from the given file fgetw()

used for reading the current position of a file ftell()

sets an intended file pointer to the file’s beginning itself rewind()

You might also like