CS001115 - Reading and writing binary files in Open C++
From Forum Nokia Wiki
| ID | CS001115 | Creation date | October 2, 2008 |
| Platform | S60 3rd Edition, FP2 | Tested on devices | Nokia 6220 Classic |
| Category | Open C/C++ | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): ifstream, ofstream, ifstream::read(), ifstream::close(), ofstream::write(), ofstream::close() |
Overview
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:
STATICLIBRARY libcrt0.lib LIBRARY libstdcpp.lib LIBRARY libc.lib LIBRARY euser.lib
Source file
#include <fstream> //ifstream, ofstream #include <iostream> //cout using namespace std; //ExampleStruct typedef struct { char chr; int num; } ExampleStruct; //ExampleClass class ExampleClass { public: ExampleClass(): m_chr(' '), m_num(0){} ExampleClass(char c, int n): m_chr(c), m_num(n){} friend ostream& operator<<(ostream&, const ExampleClass&); private: 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)); file.close(); } 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; file.close(); } int main() { write_binary_output_example(); read_binary_input_example(); //getchar(); return 0; }
Postconditions
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
- CS001114 - Reading and writing text files in Open C++
- CS001116 - Checking the file I/O status in Open C++
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading and Writing Files | Stengun | Mobile Java General | 5 | 2004-03-15 16:20 |
| Writing in A File using J2ME | jinalkathiara | Mobile Java General | 1 | 2005-09-22 12:25 |
| Binary SMS | jthieble | Multimodecards | 2 | 2003-05-31 14:35 |
| Reading Text Messages from Inbox | saadmansur | Symbian Networking & Messaging | 13 | 2007-02-08 17:06 |
| question where save file | silviuccia | General Symbian C++ | 15 | 2008-02-13 16:57 |

