#include <windows.h>
#include <tchar.h>
#include <cassert>
//
// 读写文件的简单API函数封装
// CFileBaseHelper
// |
// -----------------
// | |
// CFileReadHelper CFileWriteHelper
//
/
//基类
enum FileTextType
{
enANSI, //
enUTF8, //EF BB BF
enUnicode, //FF FE
enUnicodeBigEndian, //FE FF
enUnknown
};
class CFileBaseHelper
{
public:
CFileBaseHelper( LPCTSTR lpFileName);
~CFileBaseHelper();
void CloseFile();
protected:
FileTextType GetFileTextType(LPCTSTR lpFileName );
FileTextType m_fttTextType;
HANDLE m_hFile;
};
CFileBaseHelper::CFileBaseHelper( LPCTSTR lpFileName )
:m_fttTextType( enUnknown ), m_hFile( INVALID_HANDLE_VALUE )
{
assert( lpFileName != NULL );
if( lpFileName == NULL )
{
return;
}
m_fttTextType = GetFileTextType(lpFileName);
//打开文件
m_hFile = CreateFile( lpFileName,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL );
}
CFileBaseHelper::~CFileBaseHelper()
{
CloseFile();
}
void CFileBaseHelper::CloseFile()
{
if( m_hFile != INVALID_HANDLE_VALUE )
{
CloseHandle( m_hFile );
m_hFile = INVALID_HANDLE_VALUE;
}
}
/*****************************************************************
* 时 间: [2015年11月1日]
* 作 者:shanql
* 函数描述:获取文本类型
* 函数参数:
* 函数返回:
*****************************************************************/
FileTextType CFileBaseHelper::GetFileTextType( LPCTSTR lpFileName )
{
if( !lpFileName )
{
return enUnknown;
}
FileTextType fttTextType = enUnknown;
//open file to read three byte
HANDLE hFile = CreateFile( lpFileName,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING, NULL, NULL );
BYTE cbFlag[3] = { 0 };
DWORD dwByteRead = 0;
if( INVALID_HANDLE_VALUE != hFile &&
ReadFile( hFile, cbFlag, 3, &dwByteRead, NULL ))
{
if( cbFlag[0] == 0xFF &&
cbFlag[1] == 0xFE )
{
fttTextType = enUnicode;
}
else if ( cbFlag[0] == 0xFE &&
cbFlag[1] == 0xFF )
{
fttTextType = enUnicodeBigEndian;
}
else if ( cbFlag[0] == 0xEF &&
cbFlag[1] == 0xBB &&
cbFlag[2] == 0xBF )
{
fttTextType = enUTF8;
}
else
{
fttTextType = enANSI;
}
}
CloseHandle( hFile );
return fttTextType;
}
///
//读文件类
class CFileReadHelper:public CFileBaseHelper
{
public:
CFileReadHelper( LPCTSTR lpFileName);
BOOL ReadFile( LPVOID lpBuffer, DWORD dwByteToRead, LPDWORD lpByteRead );
};
CFileReadHelper::CFileReadHelper( LPCTSTR lpFileName )
:CFileBaseHelper( lpFileName )
{
}
BOOL CFileReadHelper::ReadFile( LPVOID lpBuffer, DWORD dwByteToRead, LPDWORD lpByteRead )
{
assert( m_hFile != INVALID_HANDLE_VALUE );
if( m_hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
assert( lpBuffer != NULL );
if( lpBuffer == NULL )
{
return FALSE;
}
BOOL bReadResult = FALSE;
switch( m_fttTextType )
{
case enANSI:
{
bReadResult = ::ReadFile( m_hFile,
lpBuffer, dwByteToRead, lpByteRead, NULL );
break;
}
case enUnicode:
case enUnicodeBigEndian:
{
//跳过前2个标记字节
if( INVALID_SET_FILE_POINTER == SetFilePointer( m_hFile, 2, 0, FILE_BEGIN) )
{
if( GetLastError() != NOERROR )
{
return FALSE;
}
}
bReadResult = ::ReadFile( m_hFile,
lpBuffer, dwByteToRead, lpByteRead, NULL );
break;
}
case enUTF8:
{
//跳过前3个标记字节
if( INVALID_SET_FILE_POINTER == SetFilePointer( m_hFile, 3, 0, FILE_BEGIN) )
{
if( GetLastError() != NOERROR )
{
return FALSE;
}
}
bReadResult = ::ReadFile( m_hFile,
lpBuffer, dwByteToRead, lpByteRead, NULL );
break;
}
default:
{
assert( false );
return FALSE;
}
}
return bReadResult;
}
//
//写文件类
class CFileWriteHelper : public CFileBaseHelper
{
public:
CFileWriteHelper( LPCTSTR lpFileName);
BOOL WriteFile( LPVOID lpBuffer, DWORD dwByteToWrite, LPDWORD lpByteWrite );
};
CFileWriteHelper::CFileWriteHelper( LPCTSTR lpFileName )
:CFileBaseHelper( lpFileName )
{
}
BOOL CFileWriteHelper::WriteFile( LPVOID lpBuffer, DWORD dwByteToWrite, LPDWORD lpByteWrite )
{
assert( m_hFile != INVALID_HANDLE_VALUE );
if( m_hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
assert( lpBuffer != NULL );
if( lpBuffer == NULL )
{
return FALSE;
}
BOOL bWriteResult = FALSE;
switch( m_fttTextType )
{
case enANSI:
{
bWriteResult = ::WriteFile( m_hFile,
lpBuffer, dwByteToWrite, lpByteWrite, NULL );
break;
}
case enUnicode:
{
if( INVALID_SET_FILE_POINTER == SetFilePointer( m_hFile, 0, 0, FILE_BEGIN) )
{
if( GetLastError() != NOERROR )
{
return FALSE;
}
}
//在文件头先写入两个字节的标记 FF FE
DWORD dwWriteHead = 0;
BYTE cbHeadFlag[] = { 0xFF, 0xFE };
if( !::WriteFile( m_hFile, cbHeadFlag, 2, &dwWriteHead, NULL ))
{
return FALSE;
}
bWriteResult = ::WriteFile( m_hFile, lpBuffer, dwByteToWrite, lpByteWrite, NULL );
break;
}
case enUnicodeBigEndian:
{
if( INVALID_SET_FILE_POINTER == SetFilePointer( m_hFile, 0, 0, FILE_BEGIN) )
{
if( GetLastError() != NOERROR )
{
return FALSE;
}
}
//在文件头先写入两个字节的标记 FE FF
DWORD dwWriteHead = 0;
BYTE cbHeadFlag[] = { 0xFE, 0xFF };
if( !::WriteFile( m_hFile, cbHeadFlag, 2, &dwWriteHead, NULL ))
{
return FALSE;
}
bWriteResult = ::WriteFile( m_hFile, lpBuffer, dwByteToWrite, lpByteWrite, NULL );
break;
}
case enUTF8:
{
if( INVALID_SET_FILE_POINTER == SetFilePointer( m_hFile, 0, 0, FILE_BEGIN) )
{
if( GetLastError() != NOERROR )
{
return FALSE;
}
}
//在文件头先写入3个字节的标记 EF BB BF
DWORD dwWriteHead = 0;
BYTE cbHeadFlag[] = { 0xEF, 0xBB, 0xBF };
if( !::WriteFile( m_hFile, cbHeadFlag, 3, &dwWriteHead, NULL ))
{
return FALSE;
}
bWriteResult = ::WriteFile( m_hFile, lpBuffer, dwByteToWrite, lpByteWrite, NULL );
break;
}
default:
{
assert( false );
return FALSE;
}
}
return bWriteResult;
}
//测试函数
void ReverseFileTest( LPCTSTR lpFileName )
{
assert( lpFileName != NULL );
if( lpFileName == NULL )
{
return;
}
CFileReadHelper tmpFileRead( lpFileName );
BYTE szReadBuffer[1024] = { 0 };
DWORD dwByteRead = 0;
if( !tmpFileRead.ReadFile( szReadBuffer, sizeof(szReadBuffer), &dwByteRead ) )
{
_tprintf( TEXT("read file failed.\r\n"));
return;
}
tmpFileRead.CloseFile();//可以不调用,析构时会自动调用
//反转文件内容
INT nStart = 0;
INT nEnd = dwByteRead-1;
BYTE cbTemp;
while( nStart < nEnd )
{
cbTemp = szReadBuffer[nStart];
szReadBuffer[nStart] = szReadBuffer[nEnd];
szReadBuffer[nEnd] = cbTemp;
++nStart;
--nEnd;
}
//写入文件
CFileWriteHelper tmpFileWrite( lpFileName );
DWORD dwByteWrite = 0;
if( !tmpFileWrite.WriteFile( szReadBuffer, dwByteRead, &dwByteWrite ) )
{
_tprintf( TEXT("write file failed.\r\n") );
return;
}
tmpFileWrite.CloseFile();//可以不调用,析构时会自动调用
}
int main()
{
ReverseFileTest(TEXT("test_ansi.txt"));
ReverseFileTest(TEXT("test_unicode.txt"));
ReverseFileTest(TEXT("test_unicode_big_endian.txt"));
ReverseFileTest(TEXT("test_utf8.txt"));
return 0;
}
作者:山丘儿
标签:return,BIG,lpBuffer,FALSE,lpFileName,UNICODE,UTF,NULL,hFile From: https://blog.51cto.com/u_15905375/5919600