C++
File I/O with binary files using fstream class is a simple task.
fstream class has the capability to do both Input as well as
Output operations i.e., read and write. All types of
operations like reading/ writing of characters, strings, lines
and not to mention buffered I/O are supported by fstream.
Operating systems store the files in binary file format.
Computers can deal with only binary numbers. But binary files
are not readable by humans. Our level of comfort lies only
with proper ASCII or UNICODE characters. This article deals
with how C++ File I/O class fstream can be used for reading and
writing binary files. For ASCII file operations in C++, refer
to C++ Text file
I/O article.
For our C++ File I/O binary file examples, we assume
a struct WebSites with two members as follows.
// Struct for C++
File I/O binary file sample
struct WebSites
{
char
SiteName[100];
int
Rank;
};
Write operations in C++ Binary File I/O:
There are some important points to be
noted while doing a write operation.
- The file has to be opened in output and binary mode
using the flags ios::out (output mode) and ios::binary( binary mode)
- The function write takes two parameters. The
first parameter is of type char * for the data to be written
and the second is of type int asking for the size of data to
be written to the binary file.
- File has to be closed at the end.
// Sample for
C++ File I/O binary file write
void write_to_binary_file(WebSites p_Data)
{
fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
binary_file.write(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
binary_file.close();
}
The above C++ File I/O binary sample
function writes some data to the function. The file is
opened in output and binary mode with ios::out and
ios::binary. There is one more specifier ios::app,
which tells the Operating system that the file is also
opened in append mode. This means any new set of data
will be appended to the end of file.
Also the write function used above,
needs the parameter as a character pointer type. So we use a
type converter reinterpret_cast to typecast the structure
into char* type.
Read operations in C++ Binary File I/O:
This also has a similar flow of operations
as above. The only difference is to open the file using
ios::in, which opens the file in read mode.
// Sample for C++ File I/O binary file read
void read_from_binary_file()
{
WebSites p_Data;
fstream binary_file("c:\\test.dat",ios::binary|ios::in);
binary_file.read(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
binary_file.close();
cout<<p_Data.SiteName<<endl;
cout<<"Rank
:"<< p_Data.Rank<<endl;
}
Most of the
programs will usually go for ASCII text mode files only. But
there will be some occasions where the C++ File I/O with
binary files will be very useful.
This " C++ File I/O using binary files" article only gives some very basic
samples for read and write. Advanced C++ File I/O operations
like seek, checking the file pointer validity etc., are also
needed to be learnt while writing bigger programs.