- /* bstream.h */
-
#include <iostream>
-
#include <fstream>
-
-
using namespace std;
-
-
// Binary output file stream
-
-
class bofstream:public ofstream {
-
public:
-
bofstream(const char *fn)
-
:ofstream(fn, ios::out|ios::binary) { }
-
void writeBytes(const void *, int);
-
template < class T>
-
bofstream & operator<< (const T & data);
-
};
-
-
template <class T>
-
bofstream & bofstream::operator<< (const T & data) //调用时会带this指针,因为返回了bofstream对象,与下面的重载区别
-
{
-
writeBytes(&data, sizeof(data));
-
return * this;
-
}
-
-
//Binary input file stream
-
class bifstream:public ifstream {
-
public:
-
bifstream(const char *fn)
-
:ifstream(fn, ios::in|ios::binary) { }
-
void readBytes(void *, int);
-
template <class T>
-
bifstream & operator>> (T & data);
-
};
-
-
template <class T>
-
bifstream & bifstream::operator>> (T & data)
-
{
-
readBytes(&data, sizeof(data));
-
return * this;
- }
/* bstream.cpp */
#include "bstream.h"
void bofstream::writeBytes(const void * p, int len)
{
if (!p) return;
if (len <= 0) return;
write((char *)p, len);
}
void bifstream::readBytes(void *p,int len)
{
if(!p) return;
if (len <= 0) return;
read((char *)p, len);
}
使用了模板函数,其它均和前例保持一样。。
/* tbdouble.cpp */
/* 基于模板类的测试实例*/
#include
#include
#include "bstream.h"
using namespace std;
#define FILENAME "tbdouble.dat"
int main()
{
//construct binary output file
bofstream bofs(FILENAME);
if (!bofs) {
cerr << "Error:unable to write to " << FILENAME <
}
//write values and close file
double d = 3.14159;
bofs << d << d * d << d * d * d;
bofs.close();
//construct binary input file
bifstream bifs(FILENAME);
if (!bifs) {
cerr << "Error: uanble to open " << FILENAME << endl;
exit(2);
}
//Read and display values from file
int count = 0;
cout.precision(8);
bifs >> d; // reading data by sizeof(d) byte
while (!bifs.eof()) {
cout << ++count << ": " << d << endl;
bifs >> d;
}
return 0;
}
对类对象的读取,写入。。。。
- /* tanyclass.h
-
* 使用类读取和写入二进制形式的类
-
* 对象
-
*/
-
#include <iostream>
-
-
using namespace std;
-
-
class TAnyClass {
-
private:
-
int x;
-
int y;
-
public:
-
friend ostream & operator<< (ostream &, const TAnyClass &); //调用时不带this指针,与上面的区别。声明为友元函数,可以使ostream类函数访问类的私有变量.调用的是标准的cin,cout istream/ostream
-
friend istream & operator>> (istream &, TAnyClass &);
-
public:
-
TAnyClass():x(0), y(0) { }
-
TAnyClass(int x, int y):x(x), y(y) { }
- };
/* tanyclass.cpp */
#include
#include "tanyclass.h"
using namespace std;
//Implements over loaded ostream output operator
ostream & operator<< (ostream &os, const TAnyClass &c)
{
os << " - - TAnyClass object - -" << endl;
os << "x == " << c.x << "; ";
os << "y == " << c.y << endl;
return os;
}
//Implements overloaded istream input operator
istream & operator>> (istream &is, TAnyClass &c)
{
cout << "Enter value for X: ";
is >> c.x;
cout << "Enter value for Y: ";
is >> c.y;
return is;
}
#include
#include
#include "tanyclass.h"
#include "bstream.h"
using namespace std;
#define FILENAME "tbobj.dat"
int main()
{
bofstream bofs(FILENAME);
if (!bofs) {
cerr << "Error: unable to write to " << FILENAME << endl;
exit(1);
}
TAnyClass obj; //Default object
//Prompt user to enter object values
cin >> obj;
//Write object to disk and close file
cout << "Writing object to disk" << endl;
bofs << obj; //如何确定调用哪个重载函数 "<<" ?
bofs.close();
//construct binary input file
cout << "Operating file" << endl;
bifstream bifs(FILENAME);
if (!bifs) {
cerr << "Error:unable to open " << FILENAME << endl;
exit(2);
}
//Read object from file. Use a new TAnyClass
//object to be sure values are new.
cout << "Reading object from disk" << endl;
TAnyClass newObject;
bifs >> newObject;
cout << "Object from disk" << endl;
cout << newObject;
bifs.close();
return 0;
}
g++ -g tbobj.cpp bstream.o tanyclass.o -o tbobj