Binary Input Output:
In binary input/output, data or records are stored into files in binary format. For example, in binary format, each integer takes two bytes. Thus the integer “30765” will take only two bytes on the disk but in case of formatted input/output, this integer takes 5 bytes of space in the memory as well as on the disk.
The binary format is commonly used to organize large amount of data in files on the disk.
Opening Files in Binary Mode:
To read/write data in binary format, the file is opened in binary mode. The “ios::binary” parameter is used to specify the binary mode. For example, to open an output file “records.dat” in binary mode, the statement is written as:
The object “rec” can also be created without attaching a data tile. The data tile can be attached afterwards with the object by using the member function “open”.
Closing Binary Files:
In other input/output modes, the opened files are automatically closed when a program terminates. But the file that is opened in binary mode must be closed before the end of the program otherwise the data into the file might be destroyed. To close a file, the “close” member function of the i/o stream object is used. The syntax to close a binary tile is:
object.close ( );
Where
object: represents the objects created for accessing data in the file.
comment closed