Loading....
Coupon Accepted Successfully!

 
Binary And Ascii Files
The insertion and extraction operators known as stream operators handle formatted data. The programmer needs to format data in order to represent them in a suitable manner. The description of formatted and unformatted data is given in Chapter 2. ASCII codes are used by the I/O devices to share or pass data to the computer system, but the central processing unit (CPU) manipulates the data using binary numbers, that is, 0 and 1. For this reason, it is essential to convert the data while accepting data from input devices and displaying the data on output devices. Consider the following statements:
 

cout<<k; // Displays value of k on screen

cin>>k; // Reads value for k from keyboard

 
Here, k is an integer variable. The operator << converts the value of the integer variable k into a stream of ASCII characters. In the same manner, the << operator converts the ASCII characters entered by the user into binary form. The data are entered through the keyboard, which is a standard input device. For example, you entered 21. The stream operator >> gets ASCII codes of the individual digits of the entered number 21, that is, 50 and 49. The ASCII codes of 2 and 1 are 50 and 49, respectively. The stream operator >> converts the ASCII value into its equivalent binary format and assigns it to the variable k. The stream operator << converts the value of k (21) that is stored in the binary format into its equivalent ASCII codes, that is, 50 and 49. Figure shows a representation of integer numbers in ASCII and binary formats.
 
Fig: Representation in binary and ASCII formats
 

16.16 Write a program to demonstrate that the data is read from the file using ASCII format.

#include<fstream.h>

#include<constream.h>

int main()

{

clrscr();

char c;

ifstream in(“data”);

if (!in)

{

cerr<<“ Error in opening file.”;
return 1;
}
while (in.eof()==0)
{
cout<<(char)in.get();
}
return 0;

}

OUTPUT
PROGRAMMING WITH ANSI AND TURBO-C
 
Explanation: In the above program, the data file is opened in read mode. The file already exists. Using get() member function of the ifstream class, the contents of the file are read and displayed. Consider the following statement:
 
cout<<(char)in.get();
 
The get() function reads data from the file in the ASCII format. Hence, it is necessary to convert the ASCII number into an equivalent character. The typecasting format (char) converts the ASCII number into an equivalent character. In case the conversion is not done, the output would be as follows:
 
8082797182657777737871328773847232657883733265786832848582667967-1
 
The above displayed are ASCII numbers, and –1 at the end indicates the end of the file.
 
After typecasting, the original string will be as shown in the output.

The write() and read() functions

The data entered by the user are represented in the ASCII format. However, the computer can understand only the machine format, that is, 0 and 1. When data are stored in the text, format numbers are stored as characters and occupy more memory space. The functions put() andget()read/ write a character. The data are stored in the file in character format. If a large amount of numeric data are stored in the file, they will occupy more space. Hence, using put() and get()creates disadvantages.
 
This limitation can be overcome using write() and read() functions. The write() and read()functions use the binary format of data while in operation. In the binary format, the data representation is same in both the file and the system. Figure shows the difference between the ASCII and binary format. The bytes required to store an integer in text form depend on its size, whereas in the binary format the size is fixed. The binary form is accurate and allows quick read and write operations, because no conversion takes places during operations. The formats of the write() and read() function are as given below.
 

in.read((char *) & P, sizeof(P));

out.write((char *) & P, sizeof(P));

 
These functions have two parameters. The first parameter is the address of the variable P. The second is the size of the variable P in bytes. The address of the variable is converted into char type. Consider the following program:
 

16.17 Write a program to perform read and write operations using write() and read() functions.

#include<fstream.h>

#include<conio.h>

#include<string.h>

int main()

{

clrscr();

int num[]={100,105,110,120,155,250,255};

ofstream out;

out.open(“01.bin”);

out.write((char *) & num, sizeof(num));

out.close();

for (int i=0;i<7;i++) num[i]=0;

ifstream in;

in.open(“01.bin”);

in.read((char *) & num, sizeof(num));

for (i=0;i<7;i++) cout<<num[i]<<“\t”;

return 0;

}

OUTPUT
100 105 110 120 155 250 255
 
Explanation: In the above program, the integer array is initialized with 7 integer numbers. The file “01.bin” is opened. The statement out.write((char *) & num, sizeof (num)) writes the integer array in the file. The &num argument provides the base address of the array, and the second argument provides the total size of the array. The close() function closes the file. Again, the same file is opened for reading purpose. Before reading the contents of the file, the array is initialized to a zero that is not necessary. The statement in.read ((char *) & num, sizeof (num)); reads data from the file and assigns them to the integer array. The second for loop displays the contents of the integer array. The size of the file “01.bin” will be 14 bytes, that is, two bytes per integer. If the above data are stored without using thewrite() command, the size of the file will be 21 bytes.

Reading and writing class objects

The read() and write() functions perform read and write operations in a binary format that is exactly the same as an internal representation of data in the computer. Due to the capabilities of these functions, large data can be stored in a small amount of memory. Both these functions are also used to write and read class objects to and from files. During read and write operations, only data members are written to the file, and the member functions are ignored. Consider the following program:
 

16.18 Write a program to perform read and write operations with objects using write() and read() functions.

#include<fstream.h>

#include<conio.h>

class boys

{

char name [20];

int age;

float height;

public:

void get()

{

cout<< “Name:”; cin>>name;

cout<< “Age:”; cin>>age;

cout<< “Height:”; cin>>height;

}

void show()

{

cout<<“\n”<<name<<“\t”<<age <<“\t”<<height;

}

};

int main()

{

clrscr();

boys b[3];

fstream out;

out.open (“boys.doc”, ios::in | ios::out);

cout<<“\n Enter following information:\n”;

for (int i=0;i<3;i++)

{

b[i].get();

out.write ((char*) & b[i],sizeof(b[i]));

}

out.seekg(0);

cout<<“\n Entered information\n”;

cout<<“Name Age Height”;

for (i=0;i<3;i++)

{

out.read((char *) & b[i], sizeof(b[i]));

b[i].show();
}
out.close();
return 0;

}

OUTPUT
Enter following information:
Name : Kamal
Age : 24
Height : 5.4
Name : Manoj
Age : 24
Height : 5.5
Name : Rohit
Age : 21
Height : 4.5
Entered information
Name Age Height
Kamal 24 5.4
Manoj 24 5.5
Rohit 21 4.5
 
Explanation: In the above program, the class boys contains data members’ name, age, and height of char, int, and float type. The class also contains the member functions get() and show()to read and display the data. In function main(), an array of three objects is declared, that is, b [3]. The file “boys.doc” is opened in the output and input mode to write and read data. The first for loop is used to call the member function get(), and data read via the get() function is written to the file by the write() function. The same method is repeated while reading the data from the file. While reading data from the file, the read() function is used, and the member function show() displays the data on the screen.




Test Your Skills Now!
Take a Quiz now
Reviewer Name