Reading and writing binary files in Open C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: ifstream, ofstream, ifstream::read(), ifstream::close(), ofstream::write(), ofstream::close()
Created: aknyman (24 Sep 2008)
Last edited: lpvalente (26 Oct 2012)



This code snippet shows how to use Open C++ file I/O classes for binary file read and write operations. The stream class fstream can be used for both read and write file I/O operations. Ifstream is only capable of file read operations and ofstream is only capable of file write operations. A header file <fstream> must be included in the application before these classes can be used. To operate as a binary file, the open() method is called with the parameter ios::binary. These file streams have two special methods for input and output binary data: read() and write(). In addition to read/write built-in data types, these classes can be used to read/write classes and structures to the file.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <fstream>  //ifstream, ofstream
#include <iostream> //cout
using namespace std;
typedef struct
char chr;
int num;
} ExampleStruct;
class ExampleClass
ExampleClass(): m_chr(' '), m_num(0){}
ExampleClass(char c, int n): m_chr(c), m_num(n){}
friend ostream& operator<<(ostream&, const ExampleClass&);
char m_chr;
int m_num;
ostream& operator<<(ostream& out, const ExampleClass& c)
out << "c.chr:" << c.m_chr << endl;
out << "c.num:" << c.m_num << endl;
return out;
void write_binary_output_example()
char chr = 'A';
int num = 1;
ExampleStruct s = {'B', 2};
ExampleClass c('C', 3);
ofstream file("example.bin", ios::binary);
//The first parameter of method write() is
//a pointer to char representing the address of
//an array of bytes where the data elements to be
//written are taken from.
//The second parameter is an integer value that specifies
//the number of characters to be written to the memory block.
file.write((char *)(&chr), sizeof(chr));
file.write((char *)(&num), sizeof(num));
file.write((char *)(&s), sizeof(s));
file.write((char *)(&c), sizeof(c));
void read_binary_input_example()
char chr = ' ';
int num = 0;
ExampleStruct s;
ExampleClass c;
ifstream file("example.bin", ios::binary);
//The first parameter of the method read() is
//a pointer to char representing the address of
//an array of bytes where the read data elements
//are stored to.
//The second parameter is an integer value that specifies
//the number of characters to be read from the memory block.
file.read((char *)(&chr), sizeof(chr));
cout << "chr:" << chr << endl;
file.read((char *)(&num), sizeof(num));
cout << "num:" << num << endl;
file.read((char *)(&s), sizeof(s));
cout << "s.chr:" << s.chr << endl;
cout << "s.num:" << s.num << endl;
file.read((char *)(&c), sizeof(c));
cout << c << endl;
int main()
return 0;


The Open C++ I/O classes ifstream and ofstream are used to read and write binary data from/to the created file simple.bin and content of the file is displayed as standard output.

See also

This page was last modified on 26 October 2012, at 20:05.
311 page views in the last 30 days.