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

2023-10-24 10:18

本文主要是介绍win32API 读写ANSI\UNICODE\UNICODE BIG-ENDIAN\UTF-8格式文本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#include <windows.h>
#include <tchar.h>
#include <cassert>//
//			    读写文件的简单API函数封装
//                  CFileBaseHelper
//                           |
//                  -----------------
//                 |                 |
//          CFileReadHelper   CFileWriteHelper
//
///基类
enum FileTextType
{enANSI,				//enUTF8,				//EF BB BFenUnicode,			//FF FEenUnicodeBigEndian,	//FE FFenUnknown
};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 FEDWORD 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 FFDWORD 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 BFDWORD 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;
}


作者:山丘儿
转载请标明出处,谢谢。原文地址:http://blog.csdn.net/s634772208/article/details/49563341

这篇关于win32API 读写ANSI\UNICODE\UNICODE BIG-ENDIAN\UTF-8格式文本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/274477

相关文章

10. 文件的读写

10.1 文本文件 操作文件三大类: ofstream:写操作ifstream:读操作fstream:读写操作 打开方式解释ios::in为了读文件而打开文件ios::out为了写文件而打开文件,如果当前文件存在则清空当前文件在写入ios::app追加方式写文件ios::trunc如果文件存在先删除,在创建ios::ate打开文件之后令读写位置移至文件尾端ios::binary二进制方式

【STM32】SPI通信-软件与硬件读写SPI

SPI通信-软件与硬件读写SPI 软件SPI一、SPI通信协议1、SPI通信2、硬件电路3、移位示意图4、SPI时序基本单元(1)开始通信和结束通信(2)模式0---用的最多(3)模式1(4)模式2(5)模式3 5、SPI时序(1)写使能(2)指定地址写(3)指定地址读 二、W25Q64模块介绍1、W25Q64简介2、硬件电路3、W25Q64框图4、Flash操作注意事项软件SPI读写W2

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

easyui同时验证账户格式和ajax是否存在

accountName: {validator: function (value, param) {if (!/^[a-zA-Z][a-zA-Z0-9_]{3,15}$/i.test(value)) {$.fn.validatebox.defaults.rules.accountName.message = '账户名称不合法(字母开头,允许4-16字节,允许字母数字下划线)';return fal

[数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别

数据集格式:Pascal VOC格式+YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):2757 标注数量(xml文件个数):2757 标注数量(txt文件个数):2757 标注类别数:4 标注类别名称:["Platelets","RBC","WBC","sickle cell"] 每个类别标注的框数:

Level3 — PART 3 — 自然语言处理与文本分析

目录 自然语言处理概要 分词与词性标注 N-Gram 分词 分词及词性标注的难点 法则式分词法 全切分 FMM和BMM Bi-direction MM 优缺点 统计式分词法 N-Gram概率模型 HMM概率模型 词性标注(Part-of-Speech Tagging) HMM 文本挖掘概要 信息检索(Information Retrieval) 全文扫描 关键词

一步一步将PlantUML类图导出为自定义格式的XMI文件

一步一步将PlantUML类图导出为自定义格式的XMI文件 说明: 首次发表日期:2024-09-08PlantUML官网: https://plantuml.com/zh/PlantUML命令行文档: https://plantuml.com/zh/command-line#6a26f548831e6a8cPlantUML XMI文档: https://plantuml.com/zh/xmi

关于使用cspreadsheet读写EXCEL表格数据的问题

前几天项目有读写EXCEL表格的需求,我就找了大概有几种,大致分为:COM方法、ODBC方法、OLE方法、纯底层格式分析方法。由于COM方法要求必须安装有OFFICE的EXCEL组件,纯底层格式分析方法又很多功能需要自行去完善,所有最终选择了数据库的方法,用数据库的方法去存取xls格式的数据。网上有一个高手写的CSpreedSheet,看了一下提供的接口,感觉挺好用的。在使用的过程中发现几个

linux 内核提权总结(demo+exp分析) -- 任意读写(四)

hijack_modprobe_path篇 本文转自网络文章,内容均为非盈利,版权归原作者所有。 转载此文章仅为个人收藏,分享知识,如有侵权,马上删除。 原文作者:jmpcall 专栏地址:https://zhuanlan.kanxue.com/user-815036.htm     原理同hijack_prctl, 当用户执行错误格式的elf文件时内核调用call_usermod