Chinaunix首页 | 论坛 | 博客
  • 博客访问: 854947
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: C/C++

2011-11-28 11:58:42

基于模板类,模板函数的二进制文件读写,可深化适应于任何类的读写操作

  1. /* bstream.h */
  2. #include <iostream>
  3. #include <fstream>

  4. using namespace std;

  5. // Binary output file stream

  6. class bofstream:public ofstream {
  7. public:
  8.     bofstream(const char *fn)
  9.         :ofstream(fn, ios::out|ios::binary) { }
  10.     void writeBytes(const void *, int);
  11.     template < class T>
  12.     bofstream & operator<< (const T & data);
  13. };

  14. template <class T>
  15. bofstream & bofstream::operator<< (const T & data)  //调用时会带this指针,因为返回了bofstream对象,与下面的重载区别
  16. {
  17.     writeBytes(&data, sizeof(data));
  18.     return * this;
  19. }

  20. //Binary input file stream
  21. class bifstream:public ifstream {
  22. public:
  23.     bifstream(const char *fn)
  24.         :ifstream(fn, ios::in|ios::binary) { }
  25.     void readBytes(void *, int);
  26.     template <class T>
  27.     bifstream & operator>> (T & data);
  28. };

  29. template <class T>
  30. bifstream & bifstream::operator>> (T & data)
  31. {
  32.     readBytes(&data, sizeof(data));
  33.     return * this;
  34. }

/* 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 <        exit(1);
        }

    //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;
}


对类对象的读取,写入。。。

  1. /* tanyclass.h
  2. * 使用类读取和写入二进制形式的类
  3. * 对象
  4. */
  5. #include <iostream>

  6. using namespace std;

  7. class TAnyClass {
  8. private:
  9.     int x;
  10.     int y;
  11. public:
  12.     friend ostream & operator<< (ostream &, const TAnyClass &); //调用时不带this指针,与上面的区别。声明为友元函数,可以使ostream类函数访问类的私有变量.调用的是标准的cin,cout istream/ostream
  13.     friend istream & operator>> (istream &, TAnyClass &);
  14. public:
  15.     TAnyClass():x(0), y(0) { }
  16.     TAnyClass(int x, int y):x(x), y(y) { }
  17. };

/* 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     //Need exit()
#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



阅读(812) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~