File:- The information / data stored under a specific name on a storage device, is called a file.
Stream:-It refers to a sequence of bytes.
Binary file:-It is a file that contains information in the same format as it is held in memory.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Opening a file
OPENING FILE USING CONSTRUCTOR
ofstreamoutFile("sample.txt"); //output only
ifstreaminFile(“sample.txt”); //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode) ;
ofstreamoutFile;
outFile.open("sample.txt");
ifstreaminFile;
inFile.open("sample.txt");
All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open(): fstream file;
file.open ("example.bin", ios::out | ios::app | ios::binary);
Closing File
outFile.close();
inFile.close();
INPUT AND OUTPUT OPERATION
put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream.
example :
ch=file.get();
file.put(ch);
WAP write a character to the file.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char x;
clrscr();
cout<<"Enter a character";
cin>>x;
ofstream o("sample");
o.put(x);
cout<<"Character written to the file";
getch();
}
WAP read character from the file.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
char x;
clrscr();
ifstream i("sample");
if(i!=NULL)
{
x=i.get();
cout<<"The Character is "<<x;
}
else
{
Cout<<"Unable to Open the file";
exit(0);
}
getch();
}
Write a program write data till you press * and print it.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
char x;
fstream f;
clrscr();
f.open("sssit.txt",ios::out);
cout<<"Enter data till * to exit";
do
{
x=getche();
if(x=='\n')
f.put(x);
if(x=='*')
break;
else
f.put(x);
}
while(1);
cout<<"Data written to the file";
f.close();
clrscr();
f.open("sssit.txt",ios::in);
if(f==NULL)
{
cout<<"File Doesnot Exist";
exit(0);
}
else
{
Cout<<"Data is "<<endl;
while(1)
{
x=f.get();
if(x==13)
cout<<"\n";
else if(f.eof())
break;
else
cout< }
}
getch();
}
WAP write strings to the file.
#include<iostream.h>
#include<conio.h>
void main()
{
ofstream o("sam.dat");
o<<"Countrys are "<<endl;
o<<"India"<<endl;
o<<"USA"<<endl;
o<<"UK"<<endl;
o<<"CANADA"<<endl;
o.close();
}
(or)
#include <fstream.h>
void main()
{
ofstreamfout;
fout.open("out.txt");
charstr[300] = "Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; //Write string to the file.
fout< fout.close();
}
Write a Program read data from the file.
#include<fstream.h>
#include<iostream.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
char ch;
while(!fin.eof())
{
ch=fin.get();
cout<<ch;
}
fin.close();
}
Write a program print no.of characters in a file.
#include<fstream.h>
#include<iostream.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count = 0;
charch;
while(!fin.eof())
{
ch=fin.get();
count++;
}
Cout<<"Number of characters in file are "<< count;
fin.close();
}
Write a program print no.of words in a file.
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count=0;
char word[20];
clrscr();
while(!fin.eof())
{
count++;
fin>> word;
}
cout<< "Number of words in file are " << count;
fin.close();
getch();
}
Write a program to print no.of lines in a program.
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count = 0;
charstr[80];
clrscr();
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<< "Number of lines in file are " << count;
fin.close();
getch();
}
Write a program to copy one file to another.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
clrscr();
fin.open("sam.dat");
ofstreamfout;
fout.open("sample.txt");
char ch;
while(!fin.eof())
{
ch=fin.get();
fout<<ch;
}
fin.close();
fout.close();
getch();
}
write() and read() function write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
//read and write functions example
#include<iostream.h>
#include<fstream.h> #include<stdio.h>
#include<conio.h>
class Student
{
int admno;
char name[50];
public:
void setData()
{
cout<<endl<<"Enter admission no. ";
cin >> admno;
cout<<endl<<"Enter name of student ";
cin>>name;
}
void showData()
{
cout<<endl<<"Admission no. : "<<admno;
cout<<endl<<"Student Name : "<<name;
}
int retAdmno()
{
return admno;
}
};
/* * function to write in a binary file. */
void write_record()
{
ofstream outFile;
outFile.open("xxx.dat", ios::binary | ios::app);
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();
}
/* * function to display records of file */
void display()
{
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
obj.showData();
}
inFile.close();
}
/* * function to search and display from binary file */
void search(int n)
{
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
obj.showData();
}
}
inFile.close();
}
/* * function to delete a record */
void delete_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("xxx.dat");
rename("temp.dat", "xxx.dat");
}
/* * function to modify a record */
void modify_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
else
{
cout<<endl<<"Enter the new details of student";
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("xxx.dat");
rename("temp.dat", "xxx.dat");
}
void main()
{
clrscr();
//Store 4 records in file
for(int i = 1; i <= 4; i++)
write_record();
//Display all records
cout<<endl<<"List of records";
display();
//Search record
cout<<"Enter record number to display";
int rno;
cin>>rno;
search(rno);
cout<<"Enter record number to delete";
cin>>rno;
//Delete record
delete_record(rno);
cout<<"\nRecord Deleted";
display();
cout<<"Enter record number to modify";
cin>>rno;
//Modify record
cout<<endl<<"Modify Record "<<rno;
modify_record(rno);
display();
getch();
}
Stream:-It refers to a sequence of bytes.
Binary file:-It is a file that contains information in the same format as it is held in memory.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Opening a file
OPENING FILE USING CONSTRUCTOR
ofstreamoutFile("sample.txt"); //output only
ifstreaminFile(“sample.txt”); //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode) ;
ofstreamoutFile;
outFile.open("sample.txt");
ifstreaminFile;
inFile.open("sample.txt");
File mode parameter | Meaning |
---|---|
ios::app | Append to end of file |
ios::in | open file for reading only |
ios::out | open file for writing only |
ios::binary | file open in binary mode |
file.open ("example.bin", ios::out | ios::app | ios::binary);
Closing File
outFile.close();
inFile.close();
INPUT AND OUTPUT OPERATION
put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream.
example :
ch=file.get();
file.put(ch);
WAP write a character to the file.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char x;
clrscr();
cout<<"Enter a character";
cin>>x;
ofstream o("sample");
o.put(x);
cout<<"Character written to the file";
getch();
}
WAP read character from the file.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
char x;
clrscr();
ifstream i("sample");
if(i!=NULL)
{
x=i.get();
cout<<"The Character is "<<x;
}
else
{
Cout<<"Unable to Open the file";
exit(0);
}
getch();
}
Write a program write data till you press * and print it.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
char x;
fstream f;
clrscr();
f.open("sssit.txt",ios::out);
cout<<"Enter data till * to exit";
do
{
x=getche();
if(x=='\n')
f.put(x);
if(x=='*')
break;
else
f.put(x);
}
while(1);
cout<<"Data written to the file";
f.close();
clrscr();
f.open("sssit.txt",ios::in);
if(f==NULL)
{
cout<<"File Doesnot Exist";
exit(0);
}
else
{
Cout<<"Data is "<<endl;
while(1)
{
x=f.get();
if(x==13)
cout<<"\n";
else if(f.eof())
break;
else
cout<
}
getch();
}
WAP write strings to the file.
#include<iostream.h>
#include<conio.h>
void main()
{
ofstream o("sam.dat");
o<<"Countrys are "<<endl;
o<<"India"<<endl;
o<<"USA"<<endl;
o<<"UK"<<endl;
o<<"CANADA"<<endl;
o.close();
}
(or)
#include <fstream.h>
void main()
{
ofstreamfout;
fout.open("out.txt");
charstr[300] = "Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; //Write string to the file.
fout<
}
Write a Program read data from the file.
#include<fstream.h>
#include<iostream.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
char ch;
while(!fin.eof())
{
ch=fin.get();
cout<<ch;
}
fin.close();
}
Write a program print no.of characters in a file.
#include<fstream.h>
#include<iostream.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count = 0;
charch;
while(!fin.eof())
{
ch=fin.get();
count++;
}
Cout<<"Number of characters in file are "<< count;
fin.close();
}
Write a program print no.of words in a file.
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count=0;
char word[20];
clrscr();
while(!fin.eof())
{
count++;
fin>> word;
}
cout<< "Number of words in file are " << count;
fin.close();
getch();
}
Write a program to print no.of lines in a program.
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
ifstream fin;
fin.open("sam.dat");
int count = 0;
charstr[80];
clrscr();
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<< "Number of lines in file are " << count;
fin.close();
getch();
}
Write a program to copy one file to another.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
clrscr();
fin.open("sam.dat");
ofstreamfout;
fout.open("sample.txt");
char ch;
while(!fin.eof())
{
ch=fin.get();
fout<<ch;
}
fin.close();
fout.close();
getch();
}
write() and read() function write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
//read and write functions example
#include<iostream.h>
#include<fstream.h> #include<stdio.h>
#include<conio.h>
class Student
{
int admno;
char name[50];
public:
void setData()
{
cout<<endl<<"Enter admission no. ";
cin >> admno;
cout<<endl<<"Enter name of student ";
cin>>name;
}
void showData()
{
cout<<endl<<"Admission no. : "<<admno;
cout<<endl<<"Student Name : "<<name;
}
int retAdmno()
{
return admno;
}
};
/* * function to write in a binary file. */
void write_record()
{
ofstream outFile;
outFile.open("xxx.dat", ios::binary | ios::app);
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();
}
/* * function to display records of file */
void display()
{
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
obj.showData();
}
inFile.close();
}
/* * function to search and display from binary file */
void search(int n)
{
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
obj.showData();
}
}
inFile.close();
}
/* * function to delete a record */
void delete_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("xxx.dat");
rename("temp.dat", "xxx.dat");
}
/* * function to modify a record */
void modify_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("xxx.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
else
{
cout<<endl<<"Enter the new details of student";
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("xxx.dat");
rename("temp.dat", "xxx.dat");
}
void main()
{
clrscr();
//Store 4 records in file
for(int i = 1; i <= 4; i++)
write_record();
//Display all records
cout<<endl<<"List of records";
display();
//Search record
cout<<"Enter record number to display";
int rno;
cin>>rno;
search(rno);
cout<<"Enter record number to delete";
cin>>rno;
//Delete record
delete_record(rno);
cout<<"\nRecord Deleted";
display();
cout<<"Enter record number to modify";
cin>>rno;
//Modify record
cout<<endl<<"Modify Record "<<rno;
modify_record(rno);
display();
getch();
}