首页 > 其他分享 >win32API 读写ANSI\UNICODE\UNICODE BIG-ENDIAN\UTF-8格式文本

win32API 读写ANSI\UNICODE\UNICODE BIG-ENDIAN\UTF-8格式文本

时间:2022-12-07 15:36:38浏览次数:37  
标签:return BIG lpBuffer FALSE lpFileName UNICODE UTF NULL hFile


#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

相关文章

  • BigDecimal multiply乘法运算
    BigDecimalmultiply乘法运算packagecom.example.core.mydemo;importjava.math.BigDecimal;publicclassMulTest{publicstaticvoidmain(String[]args)......
  • BigDecimal
    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的数......
  • 详解 Redis 中 big keys 发现和解决
    在使用Redis时,可能会出现请求响应慢、网络卡顿、数据丢失的情况。排查问题的时候,发现是bigkeys的问题。什么是bigkeys在Redis中,一个字符串类型最大可以达到512......
  • Got a packet bigger than 'max_allowed_packet' bytes,cpp mysql insert multiple va
     YoucanalsologintoMySQLconsoleandrunthefollowingcommandtosetmax_allowed_packetsizeforthedurationofthesession.setglobalnet_buffer_leng......
  • Java中的BigDecimal比较大于小于等于,四舍五入保留几位(setScale方法详解),加减乘除取余
    本文主要讲解BigDecimal的比较运算,保留精度和取整和基础运算,BigDecimal与其他数据类型转换。比较运算比较num1是否大于num2publicstaticbooleangt(@NotNullBigD......
  • 网页返回unicode源码 python解码详细步骤
    刚入门python!记录一下网页返回源码,中文部分被unicode编码,python如何处理1.先提取编码后的数据(如果不提取正篇源码直接unicode解码,解码方法无法识别)这个步骤属于逻辑......
  • mysql字符集utf8和utf8mb4的使用问题
    一、MySQL中length()、char_length()的区别和用法char_length(str)计算单位:字符不管汉字还是数字或者是字母都算是一个字符length(str)计算单位:字节utf8编码:一个汉字三个字......
  • JS对Unicode编码和解码
    JavaScript编码和解码方法方法说明escape()使用转义序列替换某些字符来对字符串进行编码unescape()对使用escape()编码的字符串进行解码encodeURI()......
  • 这就是搜索引擎(6) 云存储之BigTable
    0.背景BigTable是一个负责管理海量结构化或者半结构化数据的分布式存储系统。在Google的云存储体系中处于核心地位,起到了承上启下的作用。之前说的GFS是一个分布式的海量......
  • @DecimalMax注解 : does not represent a valid BigDecimal format
    一个小疏忽,导致踩了半天的坑。。。。。这是在由于实体类里面使用了​​@DecimalMax​​​注解,然后在使用​​ValidatorUtils.validateEntity();​​进行参数验证的时候报的......