You are on page 1of 28

File Handling in C

Definition
 A file represents a sequence of bytes on the
disk where a group of related data is stored.
 File is created for permanent storage of data.
Advantage of File:
 It will contain the data even after program exit.
Normally we use variable or array to store
data, but data is lost after program exit.
Variables and arrays are non-permanent
storage medium whereas file is permanent
storage medium.
 File streams are used to carry the
communication between devices and
program.
 The stream is the flow of data in bytes in
sequence.
 Input stream extracts data from the file
and transfers it to the program while the
output stream stores the data into the
file provided by the program.
Functions for file handling
 There are many functions in C library to open, read, write, search
and close file. A list of file functions are given below:
Opening File: fopen()
 The fopen() function is used to create a new
file or to open an existing file.
 Syntax:

 Here filename is the name of the file to be


opened and mode specifies the purpose of
opening the file.
 *fp is the FILE pointer (FILE *fp), which will
hold the reference to the opened(or
created) file.
 Returns NULL if the file cannot be
opened.(file may be in protected or hidden
mode or may be in use by another program)
 You can use one of the following modes
in the fopen() function:
 Example:
FILE *fp;
fp= fopen(“data.txt”,“r”);

The fopen() performs the following tasks:


 It searches the disk for opening of the file.
 In case the file exists,it loads the file from
the disk into memory.
 If file not existing,returns NULL
 It locates the character pointer,which
points to the first character of the file.
Closing a File
 The fclose() function is used to close an
already opened file.(file opened using fopen()
should be closed after the work is over)
 Closing the file enables to wash out all its
contents from the RAM buffer.
 Syntax:

 Closes the file associated with file pointer fp.


 returns zero on success, or EOF if there is an
error in closing the file. This EOF is a constant
defined in the header file stdio.h.
 closes one file at a time.
 Example:
fclose(fp);

 In order to close all files,


Syntax:
fcloseall();
 Closes all the opened files and returns
the number of files closed.
 It does not require any argument.
File I/O
Writing File : fprintf() function

 The fprintf() function is used to write set of


characters into file. It sends formatted
output to a stream.
 Syntax:
Program to open a text file and
write some text using fprintf()
 Example:
Reading File : fscanf() function

 The fscanf() function is used to read set


of characters from file. It reads a word
from the file and returns EOF at the end
of file.
 Syntax:

fscanf(fp,”control string”,text);
Example:
C File Example: Storing employee
information
 Example to store employee information
as entered by user from console.
 Three fields stored: id,name and salary of
the employee.
Writing File : fputc() function

 The fputc() function is used to write a


single character into file. It outputs a
character to a stream.
 Syntax:
Example
Reading File : fgetc() function

 The fgetc() function returns a single


character from the file.
 It gets a character from the stream.
 It returns EOF at the end of file.
 Syntax:
Example
Writing File : fputs() function

 The fputs() function writes a line of


characters into file.
 It outputs string to a stream.
 Syntax:
Example
Reading File : fgets() function

 The fgets() function reads a line of


characters from file.
 It gets string from a stream.
 Syntax:
Example
More Examples
Difference between Append
and Write Mode
 Write (w) mode and Append (a) mode, while
opening a file are almost the same. Both are used
to write in a file. In both the modes, new file is
created if it doesn't exists already.
 The only difference they have is, when you open a
file in the write mode, the file is reset, resulting in
deletion of any data already present in the file.
 While in append mode this will not happen.
Append mode is used to append or add data to
the existing data of file(if any).
 Hence, when you open a file in Append(a) mode,
the cursor is positioned at the end of the present
data in the file.
Reading and Writing in a
Binary File
A Binary file is similar to the text file, but
it contains only large numerical data.
 fread() and fwrite() functions are used
to read and write is a binary file.

 fread()is also used in the same way, with


the same arguments like fwrite() function.
Example

You might also like