FILE HANDLING
FILE-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.
The contents of a file are not volatile.
In C language, a structure pointer of file type is use declare a
file.
FILE structure defined in header file-<stdio.h>
NEED OF FILE HANDLING
Reusability: It helps in preserving the data or information
generated after running the program.
Large storage capacity: Using files, you need not worry
about the problem of storing data in bulk.
Saves time: There are certain programs that require a lot
of input from the user. You can easily access any part of
the code with the help of certain commands.
Portability: You can easily transfer the contents of a file
from one computer system to another without having to
worry about the loss of data.
HDD
RAM
A
55
COPY file
.c
ptr SAVE
file
Types of Files
There are basically 2 distinct types of data files available in the C-
Text File-Text files are the normal .txt files. You can easily
create text files using any simple text editors such as Notepad.
The contents of text file are 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 File-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
In C language we can perform major 4 operations-
Creating a new files
Opening an existing file
Closing a file
Reading and writing information to a file.
Working with files
When working with files, you need to declare a pointer of
type file. This declaration is needed for communication
between the file and the program.
Syntax
FILE *fpointer;
C languages provides a number of built-in-function to
perform basic file operations-
function description
fopen() Create a new file or open existing file
fclose() Close a file
getc() Read a character from file
putc() Write a character to file
fscanf() Read a set of data from a file
fprintf() Write a set of data to file
getw() Read a interger from file
putw() Write a interger to file
fseek() Set the position to desire point
ftell() Give current position in the file
STEPS FOR FILE HANDLING
Step1- Opening a File or Creating a File
The fopen() function is used to create a new file or to open an
existing file.
Syntax
FILE *fptr;
fptr = fopen(“filename” , “mode”);
MODE OF FILE
mode description
r opens a text file in reading mode
w opens or create a text file in writing mode.
a opens a text file in append mode
r+ opens a text file in both reading and writing
mode
w+ opens a text file in both reading and writing
mode
a+ opens a text file in both reading and writing
mode
Step-2 close a file
The fclose() function is used to close an already opened file.
Syntax
fclose(fptr) ;
fcloseall();
DIFFERENT I/O FUNCTION OF FILE HANDLING
Character Oriented I/O Function
1-fputc( ) to write a character to file.
Syntax
fputc(ch , fp);
ch- ch is character to store
fp- fp is file pointer
2- fgetc( ) to read a character from file.
Syntax
ch=fgetc(fp);
Integer Oriented I/O Function
1-putw( ) to write any integer number to file.
Syntax
putw( i , fp);
i-integer value
fp-file pointer
2- getw( ) to read any integer number from file.
Syntax
i=getw(fp);
String Oriented I/O Function
1-fputs( ) to write a string to a file.
Syntax
fputs(buffer , fp);
buffer- Array name
2- fgets( ) to read a string from a file.
Syntax
fgets(buffer , size ,fp);
integer
Mixed Data Type I/O Function
1-fprintf( ) To write a mixed data type to file.
It takes 3 arguments.
Syntax
fprintf(fp , “control string” ,variable);
eg- fprintf(fp, “%d%d”,a,b);
2-fscanf( ) To read a mixed data type from file.
It also take 3 arguments.
Syntax
fscanf(fp , “control string” ,&variable);
eg- scanff(fp, “%d%d”,&a,&b);