我用C++的面向对象的设计方法重新编写了上个恢复BMP的位图程序.这个看起来有点软件框架的味道了吧,但代码多了不少.写这个小程序还无法体现C++语言的优势来吧.

#include "windows.h"#include "stdio.h"class Raw{private:    HANDLE f;    DWORD Num;    DWORD filesize;public:    DWORD open(LPCTSTR lp)    {        f=::CreateFile(lp,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);        if(f==INVALID_HANDLE_VALUE){return 0;}        filesize=::GetFileSize(f,NULL);        return filesize;    }    void read(int i,LPVOID lp,DWORD nNumberOfBytesToRead)    {        ::SetFilePointer(f,i,NULL,FILE_BEGIN);        ::ReadFile(f,lp,nNumberOfBytesToRead,&Num,NULL);    }    virtual int comraw(int i)=0;    void recover(int i,int fsize)    {        HANDLE fout;        char fname[20];        ::SetFilePointer(f,i,NULL,FILE_BEGIN);        char *dout=new char[fsize];        ::ReadFile(f,dout,fsize,&Num,NULL);        sprintf(fname,"%d.bmp",i);        fout=::CreateFile(fname,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);        ::WriteFile(fout,dout,fsize,&Num,NULL);        delete [] dout;        CloseHandle(fout);    }    virtual ~Raw()    {        CloseHandle(f);    }};class BmpRaw :public Raw{private:    struct BMPHEADER{        WORD ND;        WORD TYPE;        int size;        WORD r1;        WORD r2;    }bh;public:    virtual int comraw(int i)    {        read(i,&(bh.TYPE),10) ;        if(bh.TYPE==MAKEWORD('B','M')&&bh.r1==0&&bh.r2==0)        {            return bh.size;        }        else        {            return 0;        }    }};int main(){    DWORD filesize;    int fsize;    Raw *r;    BmpRaw br;    r=&br;    filesize=r->open(("img"));    if (filesize==0)    {        return 0;    }    for (int i=0;i<(int)filesize;i+=512)    {        if((fsize=r->comraw(i))!=0)        {            r->recover(i,fsize);            i=(fsize/512)*512;        }    }    return 0;}