C-7 File Handling
C-7 File Handling
to visit all the records from top to bottom. Because of the fixed length records, computer is able to
calculated the exact address of the required record.
File Access Modes :
If a file is opened with fstream class object. Then mode in which the file is to be opened is given with the
file name. Following are the few modes
Mode
ios :: in
ios :: out
ios :: app
ios :: ate
ios :: binary
ios :: out | ios :: binary
Purpose
File is to be opened in input mode
File is to be opened in output mode from the beginning of the file, contents
of the file are overwritten
File is to be opened in append mode at end of the file, records appended at
the end of file
File is to be opened in append mode at end of the file, records can be
written anywhere in the file even in the middle or at the end of file
Opens the file in binary mode
Open the file in output mode and in binary mode
Purpose
Gets input of stdin
Gets a string from stdin until terminated by newline
\n white spaces can be there in the string
Inputs the next character from input stream and
stores it in char argument
Reads input into array line from input stream max
char read is 80 or until \0 is encountered which
ever is earlier.
Input a single character from stdin but does not
echo it on the screen
Input a single character from stdin and echos it on
the screen
Gets a character from the stdin converts it into
integer and assigns it to an integer variable
eg
(c = getchar())
Reads input from the file into the addr of objects
and data read is of the size of the object
Write output into the file into the addr of objects
and data writen is of the size of the object
Puts one char on the output stream
Puts a string on the screen
Puts output on the screen
File Pointer It is a pointer to a particular byte in a file. The file pointer determines the position
in the while where the current operation is to occur.
Get Pointer is associated with the files opened in read mode.
seekg() and tellg() seekg = seek get
seekg() searches the location
ios :: beg
ios :: cur
ios :: end
tellg() returns the location of the pointer which can be stored in some variable. Eg
int location;
location = fileobject.tellg();
Put pointer is associated with the files opened in write mode.
seekp() and tellp()
1.
Examples of a program to write text files and how to read that file
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
ofstream outputfile;
ifstream inputfile;
void main()
{
clrscr();
// char str[] = "I am mr lal. How are you?";
char strg[70];
//for outputfile
outputfile.close();
char c;
ifstream inputfile("Plal.txt");
//opening file with constructor
if(inputfile)
//to check presence of file to be read
cout<<"\nThis file is in bin, see its contents\n";
//
while(!inputfile.eof())
while(inputfile.getline(strg,10))
{
//
inputfile.get(c);
//
cout<<c;
cout<<strg<<endl;
}
inputfile.close();
getch();
}
2.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
fstream file1,file2,file3;
void main()
{
clrscr();
char strg[]="Hellow how are you? I am fine, thanks.";
file1.open("Plal.txt",ios::out);
file1<<strg;
file1.close();
char c;
// Copying contents of file Plal.txt to file xyz.txt
file3.open("xyz.txt",ios::out); //open file in which to copy contents
file2.open("Plal.txt",ios::in); //open file from which to copy contents
while(!file2.eof())
//loop to read contents of file to be copied
{
file2.get(c);
//reading characters from file
file3.put(c);
//writing charaters to file
}
file2.close();
file3.close();
cout<<"\n here is the contents of xyz.txt\n";
file3.open("xyz.txt",ios::in);
while(!file3.eof())
{
file3.get(c);
cout<<c;
}
file3.close();
getch();
}
3.
Program to write structures and objects into files and then reading them
to read/write structures and objects into files, first the data structure / object are copied into the memory
stream from there they are written/read into files.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
class employee
{
private :
int empcode;
char empname[20],empdept[20],empaddr[20];
public :
void read_emp();
void show_emp();
};
void employee :: read_emp()
{
cout<<"\nEnter Emp Code = ";
cin>>empcode;
cout<<"\nEnter Emp Name = ";
gets(empname);
cout<<"\nEnter Emp Dept = ";
gets(empdept);
cout<<"\nEnter Emp Address = ";
gets(empaddr);
}
void employee :: show_emp()
{
cout<<empcode<<"\t"<<empname<<"\t"<<empdept<<"\t"<<empaddr;
}
employee emp;
fstream file1,file2,file3;
void main()
{
clrscr();
/*
//Code to writing into EMP.DAT after getting data from object emp.read_emp()
//comments can be put on reading block after writing data into emp
int nuofemp;
cout<<"\nEnter how many employees to = ";
cin>>nuofemp;
file1.open("EMP.DAT",ios::out);
int i;
for(i=0;i<nuofemp;i++)
{
emp.read_emp();
file1.write((char *)&emp,sizeof(employee));
}
file1.close();
*/
//code to read the contents of file EMP.DAT and then the contents are shown
//using the emp.show_emp() function
//comments can be put on writing block after writing data into emp
file2.open("EMP.DAT",ios::in);
while(file2)
{
file2.read((char *)&emp,sizeof(employee));
emp.show_emp();
cout<<"\n";
}
file2.close();
getch();
}
3.
Write a function program to read the data , show the data and any a record of an object stud from a
file. Use appropriate member functions.
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
#include<process.h>
fstream file1,file2;
void write_stud(int );
void read_stud(int );
void find_stud(int );
class student
{
public :
int rollno;
char name[20];
public :
void get_data();
void show_data();
};
void student :: get_data()
{
cout<<"Enter Roll NO = ";
cin>>rollno;
cout<<"Enter Name = ";
cin>>name;
}
void student :: show_data()
{
cout<<rollno<<"\t"<<name<<endl;
}
student stud;
void main()
{
clrscr();
int xrollno,noofstud;
cout<<"Enter How many students = ";
cin>>noofstud;
write_stud(noofstud);
read_stud(noofstud);
cout<<"\nEnter Rol No of find : ";
cin>>xrollno;
find_stud(xrollno);
getch();
}
void write_stud(int noofstud)
{
int i;
file1.open("STUD.TXT",ios::out);
for(i = 0;i<noofstud;i++)
{
stud.get_data();
file1.write((char *)&stud,sizeof(stud));
}
file1.close();
}
void read_stud(int noofstud)
{
int i;
file2.open("STUD.TXT",ios::in);
for(i = 0;i<noofstud;i++)
{
file2.read((char *)&stud,sizeof(stud));
stud.show_data();
}
file2.close();
}
void find_stud(int recono)
{
int found = 0;
file1.open("STUD.TXT",ios::in|ios::binary);
while(file1)
{
file1.read((char *)&stud,sizeof(stud));
if(stud.rollno == recono)
{
stud.show_data();
found = 1;
exit(0);
}
}
if(found==0)
cout<<"\nREcord not found";
}
Q 4.
Write a program to enter books from an object of the class book and display them.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>
class book
{
private :
int bookno;
char title[20];
public :
void enter_book()
{
cout<<"\nEnter Book No : ";
cin>>bookno;
cout<<"\nEnter tile of Book : ";
gets(title);
}
void show_book()
{
cout<<bookno<<" "<<title<<endl;
}
};
const int max =5;
book bk[max];
void main()
{
clrscr();
int i;
cout<<"Enter books \n";
for(i = 0;i<max;i++)
bk[i].enter_book();
for(i=0;i<max;i++)
bk[i].show_book();
getch();
}
Q5.
#include<iostream.h>
#include<stdio.h>
#include<fstream.h>
#include<conio.h>
void books();
class book
{
private :
int bookno;
char title[20];
public :
void enter_book()
{
cout<<"\nEnter Book No : ";
cin>>bookno;
cout<<"\nEnter tile of Book : ";
gets(title);
}
void show_book()
{
cout<<bookno<<" "<<title<<endl;
}
};
book bk;
void main()
{
clrscr();
books();
getch();
}
void books()
{
bk.enter_book();
bk.show_book();
}