Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Reading and writing binary files in Open C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

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 (23 Sep 2014)



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. *)(&chr), sizeof(chr));
cout << "chr:" << chr << endl; *)(&num), sizeof(num));
cout << "num:" << num << endl; *)(&s), sizeof(s));
cout << "s.chr:" << s.chr << endl;
cout << "s.num:" << s.num << endl; *)(&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 23 September 2014, at 19:49.
824 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.