File Handling
Introduction to files: File Handling
• A collection of data which is stored on a secondary device like a hard disk is known as a file.
• A file is generally used as real-life applications that contain a large amount of data.
• Why files are needed?
• When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the
program terminates.
• If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file using a
few commands in C.
• You can easily move your data from one computer to another without any changes.
Types of Files
1. Text files
2. Binary files
1. Text files
• Text files are the normal .txt files. You can easily create text files using any simple text editors such as
Notepad.
• When you open those files, you'll see all the contents within the file as plain text. You can easily edit or
delete the contents.
• They take minimum effort to maintain, are easily readable, and provide the least security and takes
bigger storage space.
Binary files
2. Binary files
• Binary files are mostly the .bin files in your computer.
• Instead of storing data in plain text, they store it in the binary form (0's and 1's).
• They can hold a higher amount of data, are not readable easily, and provides better
security than text files.
File Operations
1. Creating a new file
2. Opening an existing file
3. Reading data from file
4. Writing data to a file
5. Closing a file
Functions for file handling
No. Function Description
1 fopen( ) opens new or existing file
2 fprintf( ) write data into the file
3 fscanf( ) reads data from the file
4 fputc( ) writes a character into the file
5 fgetc( ) reads a character from file
6 fclose( ) closes the file
7 fseek( ) sets the file pointer to given position
8 fputw( ) writes an integer to file
9 fgetw( ) reads an integer from file
10 ftell( ) returns current position
11 rewind( ) sets the file pointer to the beginning of the file
Opening File: fopen()
• We must open a file before it can be read, write, or update. The fopen() function is used to
open a file.
FILE *fopen( const char * filename, const char * mode );
• The fopen function works in the following way.
• Firstly, It searches the file to be opened.
• Then, it loads the file from the disk and place it into the buffer. The buffer is used to
provide efficiency for the read operations.
• It sets up a character pointer which points to the first character of the file.
FILE *filePointer;
filePointer = fopen(“fileName.txt”, “w”);
We can use one of the following modes in the fopen() function.
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
Closing a file
• One should always close a file whenever the operations on file are over. It
means the contents and links to the file are terminated. This prevents
accidental damage to the file.
FILE *fp;
fclose (file_pointer); fp = fopen ("data.txt", “w”);
fclose (fp);
Writing data to a file
• In C, when you write to a file, newline characters '\n' must be explicitly added.
• fputc(char, file_pointer):
It writes a character to the file pointed to by file_pointer.
• fputs(str, file_pointer):
It writes a string to the file pointed to by file_pointer.
• fprintf(file_pointer, str, variable_lists):
It prints a string to the file pointed to by file_pointer. The string can optionally include format
specifiers and a list of variables variable_lists.
Reading data from file
• We can read content of a file in c using the fscanf() and fgets() and fgetc() functions. All are used
to read contents of a file.
• fgetc(file_pointer):
It returns the next character from the file pointed to by the file pointer. When the end of the file
has been reached, the EOF is sent back.
• fgets(buffer, n, file_pointer):
It reads n-1 characters from the file and stores the string in a buffer in which the NULL
character '\0' is appended as the last character.
• fscanf(file_pointer, conversion_specifiers, variable_adresses):
It is used to parse and analyze data. It reads characters from the file and assigns the input to a list
of variable pointers variable_adresses using conversion specifiers. with scanf, fscanf stops
reading a string when space or newline is encountered.