BINARY FILE OPERATIONS in C++
In C++, by default the file stream operations are performed in text mode but supports binary file operations also. A binary file is a sequential access file in which data are stored and read back one after another in the binary format instead of ASCII characters. For example, a binary file contains integer, fl oating point number, array of structures, etc. Binary file processing is well suited for the design and development of a complex data base or to read and write a binary information.
The text file created by C++ can be edited by an ordinary editor or by a word processor. The text file can easily be transferred from one computer system to another. On the other hand, a binary file is more accurate for numbers because it stores the exact internal representation of a value. There are no conversion errors or round off errors. Saving data in binary format can be faster as there is no conversion taking place while storing data to a file. The binary format data file normally takes less space.
However, binary format data file cannot be easily transferred from one computer system to another due to variations in the internal representation of the data from one computer to another. In order to open a binary file, it is required to use the following mode:
in le (“data”, ios:: binary);
The following program segment shows how to open a binary file in C++:
#include <fstream>
using namespace std;
int main()
{
ofstream out le;
out le (“data”, ios:;binary);
-------
-------
}
Example 1.
A program to open a binary file for storing a set of numbers on a specified file.
// storing data on a le
// using binary le operations
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
ofstream out le;
char fname[10];
oat x,y,temp;
cout << “ enter a le name ? \n”;
cin >> fname;
out le.open(fname,ios::out | ios::binary);
x = 1.5;
y = 10.5;
cout << “x temp ” << endl;
while ( x <= y) {
temp = x*x;
out le << x << ‘\t’ << temp << endl;
cout << x << ‘\t’ << temp << endl;
x = x+1.5;
}
out le.close();
return 0;
}
Output of the above program
enter a le name?
data
The content of the le called “data”
1.5 2.25
3 9
4.5 20.25
6 36
7.5 56.25
9 81
10.5 110.25
CLASSES AND FILE OPERATIONS
Since C++ is an OOP language, it is reasonable to study how objects can be read and written onto a file. This section presents how objects can be written to and read from the external device, normally a disk. The header file <fstream> must be included for handling the file input and output operations. The mode of file operations such as to read, to write and both to read and write should be defined. The binary file operations required to handle the input and output are carried out using the member functions get() and put() for insertion and extraction operators. The member functions read() and write() are used to read and write a stream of objects from a specified file respectively.
(a) Reading an Object from a File : The read() member function is used to get data for the stream of object from a specified file. The general syntax of the read() member function is
in le.read(( char *) &obj, sizeof(obj));
The following program segment shows how to read a class of objects from a file using read () member function:
// reading an object from a le
#include <fstream>
using namespace std;
class student_info {
protected:
char name[20];
int age;
char sex;
public:
void getdata();
void display();
};
int main()
{
student_info obj;
fstream in le;
in le.open( “data”, ios:: in );
in le.read ((char*) &obj,sizeof(obj));
-------
-------
in le.close();
}
(b) Writing an Object to a File: The write() member function is used to save the stream of objects on a specified file. The general syntax of the write() member function is:
in le.write(( char *) &obj, sizeof(obj));
The following program segment illustrates how to write an object to a file using write() member function.
//writing an object on a le
#include <fstream>
using namespace std;
class student_info {
protected:
char name[20];
int age;
char sex;
public:
void getdata();
void display();
};
int main()
{
student_info obj;
fstream out le;
out le.open( “data”, ios::out);
out le.write ((char*) &obj,sizeof(obj));
-------
-------
out le.close();
}
Example 2.
A program to read the class object of student_info such as name, age, sex, height and weight from the keyboard and to store them on a specified file using read() and write() member functions. Again, the same file is opened for reading and displaying the contents of the file on the screen.
// classes and le operations using read and write
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class student_info {
protected :
char name[20];
int age;
char sex;
oat height;
oat weight;
public :
void getdata();
void display();
};
void student_info :: getdata()
{
cout << “ Enter the following information \n”;
cout << “ name :”;
cin >> name;
cout << “ age :”;
cin >> age;
cout << “ sex :”;
cin >> sex;
cout << “ Height :”;
cin >> height ;
cout << “ Weight :”;
cin >> weight;
}
void student_info :: display()
{
cout << name << setw(5) << age << setw(10) << sex
<< setw(5) << height << setw(5) << weight << endl;
}
int main()
{
student_info obj;
fstream in le;
char fname[10];
cout << “ enter a le name to be stored ? \n”;
cin >> fname;
in le.open(fname, ios:: in | ios::out);
// reading from the keyboard
obj.getdata();
// storing onto the le
in le.open(fname, ios::out);
cout << “ storing onto the le......\n”;
in le.write ((char*) &obj,sizeof(obj));
in le.close();
// reading from the le
in le.open(fname, ios::in);
cout << “ reading from the le......\n”;
in le.read ((char*) &obj,sizeof(obj));
obj.display();
in le.close();
return 0;
}
Output of the above program
enter a le name to be stored?
data
Enter the following information
name: Ahmed.K
age: 19
sex: M
Height: 156
Weight: 50
storing onto the le......
reading from the le......
Ahmed.K 19 M 156 50
Example 3.
A program to read a set of text from a specified file using OOPs technique.
// read and display a text le
// using OOPs technique
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class abc {
public:
void leread();
};
void abc:: leread()
{
ifstream in le;
char fname[10];
char ch;
cout << “ enter a le name to be opened ?\n”;
cin >> fname;
in le.open(fname);
if (in le.fail()) {
cerr << “ No such a le exists \n”;
exit(1);
}
cout << “ reading from the le ...\n”;
while (!in le.eof()) {
ch = (char)in le.get();
cout.put(ch);
}
in le.close();
}
int main()
{
abc obj;
obj. leread();
return 0;
}
Output of the above program
enter a le name to be opened?
data.txt
reading from the le ...
this is a test
program by Ahmed
Example 4.
A program to read a set of lines from the keyboard and to store it on a specified file using OOPs technique. The same file is used to read and display its contents on the video screen.
// read and display a text le
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class abc {
public:
char a[2000];
void getdata();
void display();
void leread();
void lesave();
};
void abc :: getdata()
{
char ch;
int i = 0;
cout <<“enter a line of text and terminate with @\n”;
while ((ch = cin.get()) != ‘@’) {
a[i++] = ch;
}
a[i++] = ‘\0’;
}
void abc :: display()
{
cout << “contents of a character array \n”;
for (int i = 0; a[i] != ‘\0’; ++i)
cout.put(a[i]);
}
void abc:: leread()
{
ifstream in le;
char fname[10];
char ch;
cout << “ enter a le name to be opened ?\n”;
cin >> fname;
in le.open(fname);
if (in le.fail()) {
cerr << “ No such a le exists \n”;
exit(1);
}
cout << “ reading from the le ...\n”;
while (!in le.eof()) {
ch = (char)in le.get();
cout.put(ch);
}
in le.close();
}
void abc:: lesave()
{
ofstream out le;
char fname[10],ch;
cout << “ enter a le name to be saved ?\n”;
cin >> fname;
out le.open(fname);
if (out le.fail()) {
cerr << “ unable to create a le \n”;
exit(1);
}
cout << “ saving onto the le ...\n”;
for (int i = 0; a[i] != ‘\0’; ++i) {
ch = (char)a[i];
out le.put(ch);
}
out le.close();
}
int main()
{
abc obj;
obj.getdata();
obj.display();
obj. lesave();
obj. leread();
return 0;
}
Output of the above program
enter a line of text and terminate with @
this is
a test
program by
Sampath K Reddy
@
contents of a character array
this is
a test
program by
Sampath K Reddy
enter a le name to be saved?
data
saving onto the le ...
enter a le name to be opened?
data
reading from the le ...
this is
a test
program by
Sampath K Reddy
Example 5.
A program to read a text file and fi nd out the number of characters and lines that are stored in the given file using OOPs technique.
// nding number of characters and lines
// of a given text le
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
class abc {
public:
void leread();
};
void abc:: leread()
{
ifstream in le;
char fname[10];
char ch;
int nch,nln;
cout << “ enter a le name to be opened ?\n”;
cin >> fname;
in le.open(fname);
if (in le.fail()) {
cerr << “ No such a le exists \n”;
exit(1);
}
nch = 0; nln = 0;
cout << “ reading from the le ...\n”;
while (!in le.eof()) {
ch = (char)in le.get();
if (ch == ‘\n’)
nln++;
++nch;
cout.put(ch);
}
cout << “Characters = ” << nch-1 << “, Lines = ” << nln-1;
cout << ‘\n’;
in le.close();
}
int main()
{
abc obj;
obj. leread();
return 0;
}
Output of the above program
enter a le name to be opened?
data
reading from the le ...
this is
a test
Characters = 16, Lines = 2

0 comments
Post a Comment