FILE HANDLING / FILE OPERATIONS /DATA FILES
A file is the name of physical memory location
within secondary storage area, that is hard disk.
In implementation, when we need to read or
write the data from the secondary storage area,
then go for file operations.
By using file operations related data can be
stored permanently in the secondary storage
area.
Whenever we want to load this information
from secondary storage area then also we can
use file operations.
I/O operations are classified into 2 types.
1. Standard I/O operations.
2. Secondary I/O operations
When we are interacting with standard I/O
devices then it is called standard I/O
operations. when we are interacting with
secondary I/O devices, then it is called
secondary I/O operations.
Standard I/O AND Secondary I/O related
operations are controlled by <stdio.h>
In C, to control file operations, we are using
predefined structure called FILE.
FILE structure size is 16 bytes.
It need pointer structure variable.
fopen(): It is used to open a file in specified
mode. If file is not opened, it returns NULL.
fwrite(): It is used to write the data into a data
file.
fread(): It is used to read the data from a data
file.
fclose(): It closes the currently opened data file.
fseek(): It is used to move the file pointer to a
specified position.
ftell(): It returns the current file pointer
position.
rename(): It is used to change the filename.
remove(): It deletes a file.
Eg 1: Writing data into a data file:
#include<stdio.h>
#include<conio.h>
struct stu
{
int id;
char name[20];
}s;
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("stu6pm","a");
if(fp==NULL)printf("Unable to create file");
else
{
do
{
printf("Enter stuid, name ");
scanf("%d %s",&s.id,s.name);
fwrite(&s,sizeof(s),1,fp);
puts("One row created");
flushall();
printf("Any more students [y/n] ");
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
fclose(fp);
}
}
Eg 2: Reading data from a data file:
#include<stdio.h>
#include<conio.h>
struct stu
{
int id;
char name[20];
}s;
void main()
{
FILE *fp;
clrscr();
fp=fopen("stu2pm","r");
if(fp==NULL)printf("File not found");
else
{
puts("Id\tName");
puts("----------------------------");
fread(&s,sizeof(s),1,fp);
while(!feof(fp))
{
printf("%d\t%s\n",s.id,s.name);
fread(&s,sizeof(s),1,fp);
}
fclose(fp);
}
getch();
}
Eg 3: Searching a record:
#include<stdio.h>
#include<conio.h>
struct stu
{
int id;
char name[20];
}s;
void main()
{
FILE *fp;
int idno, found=0;
clrscr();
fp=fopen("stu2pm","r");
if(fp==NULL)printf("File not found");
else
{
printf("Enter stuid ");
scanf("%d",&idno);
fread(&s,sizeof(s),1,fp);
while(!feof(fp))
{
if(idno==s.id)
{
found=1;
puts("Id\tName");
puts("----------------------------");
printf("%d\t%s\n",s.id,s.name);
break;
}
fread(&s,sizeof(s),1,fp);
}
fclose(fp);
if(found==0)printf("Student not available");
}
getch();
}
Eg 4: Editing a record [ Update ]
#include<stdio.h>
#include<conio.h>
struct stu
{
int id;
char name[20];
}s;
void main()
{
FILE *fp;
int idno, found=0;
clrscr();
fp=fopen("stu2pm","r+");
if(fp==NULL)printf("File not found");
else
{
printf("Enter stuid ");
scanf("%d",&idno);
fread(&s,sizeof(s),1,fp);
while(!feof(fp))
{
if(idno==s.id)
{
found=1;
puts("Id\tName");
puts("----------------------------");
printf("%d\t%s\n",s.id,s.name);
printf("Enter new id, name ");
scanf("%d %s",&s.id,s.name);
fseek(fp,ftell(fp)-sizeof(s),0);
fwrite(&s,sizeof(s),1,fp);
printf("One row updated");
break;
}
fread(&s,sizeof(s),1,fp);
}
fclose(fp);
if(found==0)printf("Student not available");
}
getch();
}
Eg 5 Deleting a record:
#include<stdio.h>
#include<conio.h>
struct stu
{
int id;
char name[20];
}s;
void main()
{
FILE *fp,*t;
int idno, found=0;
char ch;
clrscr();
fp=fopen("stu2pm","r");
if(fp==NULL)printf("File not found");
else
{
printf("Enter stuid ");
scanf("%d",&idno);
fread(&s,sizeof(s),1,fp);
while(!feof(fp))
{
if(idno==s.id)
{
found=1;
puts("Id\tName");
puts("----------------------------");
printf("%d\t%s\n",s.id,s.name);
break;
}
fread(&s,sizeof(s),1,fp);
}
if(found==0)printf("Stu not available");
else
{
flushall();
printf("Ru sure to delete [y/n]");
scanf("%c",&ch);
if(ch=='n'||ch=='N')printf("Oeration
cancelled");
else
{
fp = fopen("stu2pm","r");
t = fopen("temp","w");
fread(&s,sizeof(s),1,fp);
while(!feof(fp))
{
if(idno!=s.id)fwrite(&s,sizeof(s),1,t);
fread(&s,sizeof(s),1,fp);
}
fclose(fp);
fclose(t);
remove("stu2pm");
rename("temp","stu2pm");
printf("One row deleted");
}
}
}
getch();
}
In C++ the total I/O operations are controlled
by streams.
Stream: A sequence of bytes which are having
a source or destination.
Data flow diagram in between program file
and data file:
In C all the file operations are controlled by
a predefined structure FILE of type pointer.
Here FILE size is 16 bytes.