拷贝文件的三种方法源代码,基于工厂模式的Windows和Linux文件操作的封装

行事好多天终于把这些标题化解了。呵呵,前日linux老师给大家布署了叁个作业,对window和linux的文书操作实行李包裹装,何况将那三个包装写成三个基类的派生类,通过基类调用派生类访问。从前都是用间接访问派生类的,今后用基类访谈派生类不知道怎么搞了,查了须臾间工厂方式能够帮小编做到。代码如下:在Windows和Linux
下都能运维。
/*
赵丽 电子金融学院
兑现基于工厂方式的windows和linux文件操作的包裹
*/
//head.h

(1)
使用ANSI C的库函数
 
 
 
  能够选用ANSI C的以下多少个库函数:   
 
  FILE *fopen( const char *filename, const char *mode );   
 
  int fclose( FILE *stream );
 
  size_t fread( void *buffer, size_t size, size_t count, FILE
*stream );
 
  size_t fwrite( const void *buffer, size_t size, size_t count, FILE
*stream );

#include <map>
#include <string>
#include <stdio.h>
class file;
typedef file* (*FactoryFunction)();
#ifdef WIN32
#define TEST 1
#else
#define TEST 0
#endif 
#define NAME “test”
class DeviceFactory
{
public:
static void Register(std::string name, FactoryFunction
instanceFunction)
{
m_FactoryFunctions[name] = instanceFunction;
}
static file* GetInstance(std::string name)
{
if (m_FactoryFunctions.count(name))
{
return m_FactoryFunctions[name]();
}
else
{
return NULL;
}
}
private:
static std::map<std::string, FactoryFunction>
m_FactoryFunctions;
};
std::map<std::string, FactoryFunction>
DeviceFactory::m_FactoryFunctions;

 
 示例源码如下:

class file
{
public:
virtual void open() = 0;
virtual void createfile(char* path)=0;
virtual void writefile(char* path,char inText[])=0;
virtual void readfile(char* path)=0;
//virtual void closefile()=0;
};

/*
 
  cpc.c
 
  use c library to copy file
*/

#if TEST==1
#include <windows.h>
#include <stdio.h>
DWORD  szBuffer[4];
const char inText[] = “QQ:610847323”;
char  Length;
class file_windows : public file
{
public:
void open()
{
std::cout <<“windows class”<<std::endl;
}

#include
<stdio.h>
#include
<errno.h>

void createfile(char* path)
{   
HANDLE  hFile;   
hFile=CreateFile(path,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
}

#define
BUF_SIZE    256

void writefile(char* path,char inText[])
{
HANDLE hFile;
DWORD nBytes;
hFile=CreateFile(path,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,0,NULL);
if(hFile!=INVALID_HANDLE_VALUE)
{
WriteFile(hFile,inText, lstrlen(inText),&nBytes,NULL);
CloseHandle(hFile);
}
}

int
main(int argc, char *argv[])
{
 
  FILE *in_file, *out_file;
 
  char data[BUF_SIZE];
 
  size_t bytes_in, bytes_out;
 
  long len = 0;

void readfile(char* path)
{
HANDLE hFile;
DWORD  dwBytesRead;
char buffer[4096];
hFile = CreateFile(“hello.txt”,
GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);                            
ReadFile(hFile, buffer, 4096, &dwBytesRead, NULL);
CloseHandle(hFile);     
for(int j=0;j<5;j++)
{
printf(“%c”,buffer[j]);
}
printf(“n”);
}

 
  if ( argc != 3 )
 
  {
 
      printf(“Usage: %s file1 file2n”, argv[0]);
 
      return 1;
 
  }

static file* CreateInstance()
{
static file_windows* windows0=new file_windows;
return windows0;
}
};
#else
class file_linux : public file
{
public:
void open()
{
std::cout<<“linux class”<<std::endl;
}

 
  if ( (in_file = fopen(argv[1], “rb”)) == NULL )
 
  {
 
      perror(argv[1]);
 
      return 2;
 
  }
 
  if ( (out_file = fopen(argv[2], “wb”)) == NULL )
 
  {
 
      perror(argv[2]);
 
      return 3;
 
  }

void createfile(char* path)
{   
fp=fopen(path,”w”);
}

 
  while ( (bytes_in = fread(data, 1, BUF_SIZE, in_file)) > 0
)
 
  {
 
      bytes_out = fwrite(data, 1, bytes_in, out_file);
 
      if ( bytes_in != bytes_out )
 
      {
 
          perror(“Fatal write error.n”);
 
          return 4;
 
      }
 
      len += bytes_out;
 
      printf(“copying file …. %d bytes copyn”, len);
 
  }

void readfile(char* path)
{
printf(“reading start—-n”);
FILE * stream;
char buffer[10];
int i;
stream = fopen(path,”r”);
fread(buffer,sizeof(char),5,stream);       
fclose(stream);
for(i=0;i<5;i++)
{
printf(“%c”,buffer[i]);
}
printf(“n”);
}

 
  fclose(in_file);
 
  fclose(out_file);

void writefile(char* path,char buffer[])
{
printf(“writing start—-n”);
FILE * stream;
int i;
stream = fopen(path,”w”);
fwrite(buffer,sizeof(char),5,stream);
fclose(stream);
for(i=0;i<5;i++)
{
printf(“%c”,buffer[i]);
}
printf(“n”);
}

 
  return 0;
}

/*    void open(const char* path,const char* mode)
{
FILE * fp;
fp=fopen(path,mode);
if(fp==NULL)
{
return;
}
fclose(fp);
}
*/

2.
使用Windows 32 API 函数
 
 主要运用以下多少个函数:
 
 HANDLE CreateFile( LPCTSTR lpFileName,
 
                    DWORD dwDesiredAccess,
 
                    DWORD dwShareMode,
 
                    LPSECURITY_ATTRIBUTES
lpSecurityAttributes,
 
                    DWORD dwCreationDispostion ,
 
                    DWORD dwFlagsAndAttributes,
 
                    HANDLE hTemplateFile);
 
 BOOL ReadFile( HANDLE hFile,
 
                LPVOID lpBuffer,
 
                DWORD nNumberOfBytesToRead,
 
                LPDWORD lpNumberOfBytesRead,
 
                LPOVERLAPPED lpOverlapped);
 
 BOOL WriteFile( HANDLE hFile,
 
                 LPCVOID lpBuffer,
 
                 DWORD nNumberOfBytesToWrite,
 
                 LPDWORD lpNumberOfBytesWritten,
 
                 LPOVERLAPPED lpOverlapped);

static file* CreateInstance()
{
static file_linux* linux0=new file_linux;
return linux0;
}
};
#endif

 
 示例代码如下:
  

#if TEST==1
FactoryFunction fun=&file_windows::CreateInstance;
#else
FactoryFunction fun=&file_linux::CreateInstance;
#endif

/* 
  
 
  cpw.c
 
  copy file, use windows functions
*/

//main.cpp
#include <iostream>
#include “head.h”
using namespace std;
int main()
{
DeviceFactory::Register(NAME,fun);
file* file = DeviceFactory::GetInstance(NAME);
file->open();
file->createfile(“hello0.txt”);
file->writefile(“hello.txt”,”hello”);
file->readfile(“hello.txt”);
delete file;
return 0;
}

#include
<windows.h>
#include
<stdio.h>

图片 1

#define 
  BUF_SIZE    256

发表评论

电子邮件地址不会被公开。 必填项已用*标注