Reading and writing binary files in Open C++
Contents |
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.


(no comments yet)