C++ win平台文件处理类

#ifndef __FILE_HELPER_H__
#define __FILE_HELPER_H__
#include <iostream>
#include <fstream>

class FileHelper
{
private:
	FileHelper() {};
	FileHelper(const FileHelper&) {};
	FileHelper& operator=(const FileHelper &) {};
public:
	~FileHelper();

	static FileHelper* getInstance();

	//判断文件是否存在
	virtual	bool isFileExist(const std::string & filePath);

	//创建文件
	virtual	bool createFile(const std::string & filePath);

	//读文件
	//将文件数据的buffSize字节读入到buffer中
	//若buffSize为0,则将整个文件读入buffer
	virtual bool readFile(const std::string& filePath, char* buffer, int fileSize = 0);

	//对同一个文件进行多次读取
	std::ifstream* readFileBegin(const std::string& filePath);
	void readFile(std::ifstream* file, char* buffer, int fileSize);
	void readFileEnd(std::ifstream* file);

	//写文件
	//将buffer数据的buffSize字节写入到文件中
	virtual bool writeFile(const std::string& filePath, char* buffer, int buffSize);

	//对同一个文件进行多次写入
	std::ofstream* writeFileBegin(const std::string& filePath);
	void writeFile(std::ofstream* file, char* buffer, int buffSize);
	void writeFileEnd(std::ofstream* file);

	//获取文件大小
	virtual	int getFileSize(const std::string & filePath);

	enum ePermission
	{
		eWrite = 02,
		eRead = 04,
		eReadWrite = 06
	};
	//文件是否有指定权限
	virtual bool isFilePermission(const std::string& filePath, ePermission permission);

	//删除文件
	virtual bool deleteFile(const std::string& filePath);

	//重命名文件。若已存在重名文件,则命名失败
	virtual std::string renameFile(const std::string& filePath, const std::string& newFileName);

	//以文件路径复制文件
	//源文件必须存在
	//目标文件若存在,则替换;若不存在,则创建
	virtual bool copyFile(const std::string& srcFilePath, const std::string& destFilePath);

private:
	static	FileHelper*	_pFileHelper;
};
#endif // __FILE_HELPER_H__

#include "stdafx.h"
#include "FileHelper.h"
#include "io.h"
#include <sys\stat.h> 

using namespace std;

FileHelper*	FileHelper::_pFileHelper = nullptr;

FileHelper::~FileHelper()
{
	if (_pFileHelper)
	{
		delete (_pFileHelper);
		_pFileHelper = nullptr;
	}
}

FileHelper* FileHelper::getInstance()
{
	if (_pFileHelper == nullptr)
	{
		_pFileHelper = new	FileHelper;
	}
	return	_pFileHelper;
}

bool FileHelper::isFileExist(const std::string& filePath)
{
	return	_access((char*)filePath.data(), 0) ? false : true;
}

bool FileHelper::createFile(const std::string& filePath)
{
	ofstream file;
	file.open((char*)filePath.data(), ios::binary);//创建目标文件 

	if (file.fail())	return	false;
	file.close();

	return	true;
}

bool FileHelper::readFile(const std::string& filePath, char* buffer, int fileSize)
{
	ifstream file;
	file.open((char*)filePath.data(), ios::binary);//打开目标文件

	if (file.fail())	return	false;
	if (fileSize < 0)	fileSize = getFileSize(filePath);

	file.read(buffer, fileSize);//将数据读入缓存
	file.close();

	return	true;
}

ifstream* FileHelper::readFileBegin(const std::string& filePath)
{
	ifstream* file = new ifstream;
	file->open((char*)filePath.data(), ios::binary);//打开目标文件

	if (file->fail())
	{
		delete	file;
		return	nullptr;
	}

	return	file;
}

void FileHelper::readFile(ifstream* file, char* buffer, int fileSize)
{
	file->read(buffer, fileSize);//将数据读入缓存
}

void FileHelper::readFileEnd(ifstream* file)
{
	if (file)
	{
		file->close();
		delete	file;
	}
}

bool FileHelper::writeFile(const std::string& filePath, char* buffer, int buffSize)
{
	ofstream file;
	file.open((char*)filePath.data(), ios::binary);//打开目标文件

	if (file.fail())	return	false;

	file.write(buffer, buffSize);//将数据写入文件
	file.close();

	return	true;
}

ofstream* FileHelper::writeFileBegin(const std::string& filePath)
{
	ofstream* file = new ofstream;
	file->open((char*)filePath.data(), ios::binary);//打开目标文件

	if (file->fail())
	{
		delete	file;
		return	nullptr;
	}

	return	file;
}

void FileHelper::writeFile(ofstream* file, char* buffer, int buffSize)
{

	file->write(buffer, buffSize);//将数据写入文件
}

void FileHelper::writeFileEnd(ofstream* file)
{
	if (file)
	{
		file->close();
		delete	file;
	}
}

int FileHelper::getFileSize(const std::string & filePath)
{
	struct _stat info;
	_stat((char*)filePath.data(), &info);
	return	info.st_size;
}

bool FileHelper::isFilePermission(const std::string& filePath, ePermission permission)
{
	return	_access((char*)filePath.data(), permission) ? false : true;
};

bool FileHelper::deleteFile(const std::string& filePath)
{
	if (isFileExist(filePath))
	{
		if (!remove((char*)filePath.data()))//删除成功
		{
			return	true;
		}
	}
	return	false;
}

string FileHelper::renameFile(const std::string& filePath, const std::string& newFileName)
{
	string newFilePath;
	if (isFileExist(filePath))
	{
		int indexP = filePath.find_last_of('\\');
		int indexN = filePath.find_last_of('/');
		newFilePath = filePath.substr(0, (indexP > indexN ? indexP : indexN) + 1);
		newFilePath += newFileName;
		if (rename((char*)filePath.data(), (char*)newFilePath.data()))//重命名失败
		{
			newFilePath.clear();
		}
	}
	return	newFilePath;
}

bool FileHelper::copyFile(const string& srcFilePath, const string& destFilePath)
{
	ifstream src;
	ofstream dest;
	src.open((char*)srcFilePath.data(), ios::binary);//打开源文件
	if (src.fail())//打开源文件失败
	{
		cout << "Error 1: Fail to open the source file." << endl;
		src.close();
		dest.close();
		return false;
	}
	dest.open((char*)destFilePath.data(), ios::binary);//创建目标文件 
	if (dest.fail())//创建文件失败
	{
		cout << "Error 2: Fail to create the new file." << endl;
		dest.close();
		src.close();
		return false;
	}

	dest << src.rdbuf();
	dest.close();
	src.close();
	return	true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值