静态库封装之ComFile类
ComFile.h
/*
@author:EricsT
@data:20241024
@version:V1.0
@history:
@author @data @version @content
EricsT 20241024 V1.0 新增ComFile类[common、FILE以及stream部分]
*/
#pragma once
#include <string>
#include <fstream>
using namespace std;
class ComFile
{
public:
//common
//=============================================================================================================
/*
func:判断文件是否存在
@strFilePath:文件路径
@csFilePath:文件路径
@pchFilePath:文件路径
ret:存在true,不存在false
*/
static bool isFileExists(string strFilePath);//20241024
static bool isFileExists(CString csFilePath);//20241024
static bool isFileExists(PCHAR pchFilePath);//20241024
//FILE
//=============================================================================================================
/*
func:在bin文件的指定地址处读取1个字节
@fp:文件指针
@iAddr:指定地址
ret:读取的数据
*/
static BYTE ReadByteFromAddr_Bin(FILE* fp, DWORD iAddr);//20241024
/*
func:在bin文件的当前地址处读取1个字节
@fp:文件指针
ret:读取的数据
*/
static BYTE ReadByte_Bin(FILE* fp);//20241024
/*
func:在bin文件的指定地址处读取2个字节
@fp:文件指针
@iAddr:指定地址
ret:读取的数据
*/
static WORD ReadWordFromAddr_Bin(FILE* fp, DWORD iAddr);//20241024
/*
func:在bin文件的当前地址处读取2个字节
@fp:文件指针
ret:读取的数据
*/
static WORD ReadWord_Bin(FILE* fp);//20241024
/*
func:在bin文件的指定地址处读取4个字节
@fp:文件指针
@iAddr:指定地址
ret:读取的数据
*/
static DWORD ReadDWordFromAddr_Bin(FILE* fp, DWORD iAddr);//20241024
/*
func:在bin文件的当前地址处读取4个字节
@fp:文件指针
ret:读取的数据
*/
static DWORD ReadDWord_Bin(FILE* fp);//20241024
/*
func:在bin文件的指定地址处读取指定字节
@fp:文件指针
@iAddr:指定地址
@num:指定字节
*/
static void ReadUselessByteFromAddr_Bin(FILE* fp, DWORD iAddr, DWORD num);//20241024
/*
func:在bin文件的当前地址处读取指定字节
@fp:文件指针
@num:指定字节
*/
static void ReadUselessByte_Bin(FILE* fp, DWORD num);//20241024
//stream
//=============================================================================================================
/*
func:在stream文件中读取一行
@ifs:文件流
ret:读取的内容
*/
static string GetLine_Stream(ifstream& ifs);//20241024
};
ComFile.cpp
/*
@author:EricsT
@data:20241024
@version:V1.0
*/
#include "stdafx.h"
#include "ComFile.h"
#include <io.h>
bool ComFile::isFileExists(string strFilePath)
{
if (-1 == _access(strFilePath.c_str(), 0))
return false;
return true;
}
bool ComFile::isFileExists(CString csFilePath)
{
//CStringToString
int len = csFilePath.GetLength();
PCHAR pch = new char[len + 1];
size_t pchSize = wcstombs(pch, csFilePath, len + 1);
if (pchSize == wstring::npos)
{
delete pch;
return "";
}
string strFilePath(pch);
delete pch;
if (-1 == _access(strFilePath.c_str(), 0))
return false;
return true;
}
bool ComFile::isFileExists(PCHAR pchFilePath)
{
if (-1 == _access(pchFilePath, 0))
return false;
return true;
}
BYTE ComFile::ReadByteFromAddr_Bin(FILE* fp, DWORD iAddr)
{
if (nullptr == fp)
return 0;
BYTE bRet = 0;
fseek(fp, iAddr, SEEK_SET);
fread(&bRet, sizeof(BYTE), 1, fp);
return bRet & 0xFF;
}
BYTE ComFile::ReadByte_Bin(FILE* fp)
{
if (nullptr == fp)
return 0;
BYTE bRet = 0;
fread(&bRet, sizeof(BYTE), 1, fp);
return bRet & 0xFF;
}
WORD ComFile::ReadWordFromAddr_Bin(FILE* fp, DWORD iAddr)
{
if (nullptr == fp)
return 0;
WORD wRet = 0;
fseek(fp, iAddr, SEEK_SET);
fread(&wRet, sizeof(WORD), 1, fp);
return wRet & 0xFFFF;
}
WORD ComFile::ReadWord_Bin(FILE * fp)
{
if (nullptr == fp)
return 0;
WORD wRet = 0;
fread(&wRet, sizeof(WORD), 1, fp);
return wRet & 0xFFFF;
}
DWORD ComFile::ReadDWordFromAddr_Bin(FILE* fp, DWORD iAddr)
{
if (nullptr == fp)
return 0;
DWORD dwRet = 0;
fseek(fp, iAddr, SEEK_SET);
fread(&dwRet, sizeof(DWORD), 1, fp);
return dwRet & 0xFFFFFFFF;
}
DWORD ComFile::ReadDWord_Bin(FILE* fp)
{
if (nullptr == fp)
return 0;
DWORD dwRet = 0;
fread(&dwRet, sizeof(DWORD), 1, fp);
return dwRet & 0xFFFFFFFF;
}
void ComFile::ReadUselessByteFromAddr_Bin(FILE* fp, DWORD iAddr, DWORD num)
{
if (nullptr == fp)
return;
BYTE bRet;
fseek(fp, iAddr, SEEK_SET);
for (int i = 0; i < num; i++)
fread(&bRet, sizeof(BYTE), 1, fp);
}
void ComFile::ReadUselessByte_Bin(FILE* fp, DWORD num)
{
if (nullptr == fp)
return;
BYTE bRet;
for (int i = 0; i < num; i++)
fread(&bRet, sizeof(BYTE), 1, fp);
}
std::string ComFile::GetLine_Stream(ifstream& ifs)
{
if (!ifs.is_open())
return string();
string strLine;
getline(ifs, strLine);
return strLine;
}
本文来自博客园,作者:EricsT,转载请注明原文链接:https://www.cnblogs.com/EricsT/p/18500382