最近因为要读取SQL脚本文件,用CStdioFile来读取脚本文件,却在程序调试时读取不了文件。 class CStdioFileEx: public CStdioFile virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL ); // Additional flag to allow Unicode text writing // static utility functions // -------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------- bool m_bIsUnicodeText; 实现文件如下: CStdioFileEx::CStdioFileEx(): CStdioFile() CStdioFileEx::CStdioFileEx(LPCTSTR lpszFileName,UINT nOpenFlags) BOOL CStdioFileEx::Open(LPCTSTR lpszFileName,UINT nOpenFlags,CFileException* pError /*=NULL*/) return CStdioFile::Open(lpszFileName, nOpenFlags, pError); BOOL CStdioFileEx::ReadString(CString& rString) // If at position 0, discard byte-order mark before reading // If compiled for Unicode if (!m_bIsUnicodeText) // Read as Unicode, convert to ANSI if(fgetws(pszUnicodeString, nMAX_LINE_CHARS, m_pStream)==NULL) if (pszUnicodeString) if (pszMultiByteString) // Then remove end-of-line character if in Unicode text mode return bReadData; // -------------------------------------------------------------------------------------------- // If compiled in Unicode... // If writing Unicode, no conversion needed // Copy string to Unicode buffer // Get multibyte string if (pszUnicodeString && pszMultiByteString) // Copy string to multibyte buffer if (GetUnicodeStringFromMultiByteString(pszMultiByteString, pszUnicodeString, nBufferSize, GetACP())) if (pszUnicodeString && pszMultiByteString) #endif UINT CStdioFileEx::ProcessFlags(const CString& sFilePath, UINT& nOpenFlags) // If we have writeUnicode we must have write or writeRead as well // If reading in text mode and not creating... // If it's Unicode, switch to binary mode m_nFlags = nOpenFlags; return nOpenFlags; // -------------------------------------------------------------------------------------------- // Open file in binary mode and read first character file.Close(); return bIsUnicode; unsigned long CStdioFileEx::GetCharCount() if (m_pStream) // If Unicode, remove byte order mark from count // Calc chars return nCharCount; // -------------------------------------------------------------------------------------------- try if (nReturn == 0) ASSERT(bOK); // -------------------------------------------------------------------------------------------- if (szUnicodeString && szMultiByteString) try return bGotIt;
后来看了一下文本文件格式,竟然是UNICODE格式的,原来在导出SQL脚本的时候,选项默认的是UNICODE格式。为了同时支持ANSI和UNICODE两种格式,在Codeproject站点上找到了CStdioFileEx类的代码,但在调试运行过程中发现,在生成UNICODE版本的执行文件时,运行没有错误,但在非UNICODE版本中却出现错误,原来在代码中此部分没有考虑文件读到末尾的情况,经修改,CStdioFileEx类就可以正常使用了,在读取文本文件时,自动识别ANSI和UNICODE两种格式。
实现头文件如下:
#define nUNICODE_BOM 0xFEFF // Unicode "byte order mark" which goes at start of file
#define sNEWLINE _T("/r/n") // New line characters
#define sDEFAULT_UNICODE_FILLER_CHAR "#" // Filler char used when no conversion from Unicode to local code page is possible
{
public:
CStdioFileEx();
CStdioFileEx( LPCTSTR lpszFileName, UINT nOpenFlags );
virtual BOOL ReadString(CString& rString);
virtual void WriteString( LPCTSTR lpsz );
bool IsFileUnicodeText() { return m_bIsUnicodeText; }
unsigned long GetCharCount();
static const UINT modeWriteUnicode;
//
// CStdioFileEx::GetUnicodeStringFromMultiByteString()
//
// --------------------------------------------------------------------------------------------
// Returns: bool
// Parameters: char * szMultiByteString (IN) Multi-byte input string
// wchar_t* szUnicodeString (OUT) Unicode output string
// short nUnicodeBufferSize (IN) Size of Unicode output buffer
// UINT nCodePage (IN) Code page used to perform conversion
// Default = -1 (Get local code page).
//
// Purpose: Gets a Unicode string from a MultiByte string.
// Notes: None.
// Exceptions: None.
//
static bool GetUnicodeStringFromMultiByteString(char * szMultiByteString,wchar_t* szUnicodeString,
short nUnicodeBufferSize,UINT nCodePage=-1);
//
// CStdioFileEx::GetMultiByteStringFromUnicodeString()
//
// --------------------------------------------------------------------------------------------
// Returns: BOOL
// Parameters: wchar_t * szUnicodeString (IN) Unicode input string
// char* szMultiByteString (OUT) Multibyte output string
// short nMultiByteBufferSize (IN) Multibyte buffer size
// UINT nCodePage (IN) Code page used to perform conversion
// Default = -1 (Get local code page).
//
// Purpose: Gets a MultiByte string from a Unicode string.
// Notes: .
// Exceptions: None.
//
static BOOL GetMultiByteStringFromUnicodeString(wchar_t * szUnicodeString,char* szMultiByteString,
short nMultiByteBufferSize,UINT nCodePage=-1);
// --------------------------------------------------------------------------------------------
//
// CStdioFileEx::IsFileUnicode()
//
// --------------------------------------------------------------------------------------------
// Returns: bool
// Parameters: const CString& sFilePath
//
// Purpose: Determines whether a file is Unicode by reading the first character and detecting
// whether it's the Unicode byte marker.
// Notes: None.
// Exceptions: None.
//
static bool IsFileUnicode(const CString& sFilePath);
protected:
UINT ProcessFlags(const CString& sFilePath, UINT& nOpenFlags);
UINT m_nFlags;
};
/*static*/ const UINT CStdioFileEx::modeWriteUnicode = 0x20000; // Add this flag to write in Unicode
{
m_bIsUnicodeText = false;
}
:CStdioFile(lpszFileName, ProcessFlags(lpszFileName, nOpenFlags))
{
}
{
// Process any Unicode stuff
ProcessFlags(lpszFileName, nOpenFlags);
}
{
const int nMAX_LINE_CHARS = 4096;
BOOL bReadData;
LPTSTR lpsz;
int nLen = 0; //, nMultiByteBufferLength = 0, nChars = 0;
CString sTemp;
wchar_t* pszUnicodeString = NULL;
char * pszMultiByteString= NULL;
if (!m_pStream || (GetPosition() == 0 && m_bIsUnicodeText))
{
wchar_t cDummy;
// Read(&cDummy, sizeof(_TCHAR));
Read(&cDummy, sizeof(wchar_t));
}
#ifdef _UNICODE
// Do standard stuff -- both ANSI and Unicode cases seem to work OK
bReadData = CStdioFile::ReadString(rString);
#else
{
// Do standard stuff -- read ANSI in ANSI
bReadData = CStdioFile::ReadString(rString);
}
else
{
pszUnicodeString = new wchar_t[nMAX_LINE_CHARS];
pszMultiByteString= new char[nMAX_LINE_CHARS];
{
bReadData=FALSE;
}
else
{
bReadData=TRUE;
if (GetMultiByteStringFromUnicodeString(pszUnicodeString, pszMultiByteString, nMAX_LINE_CHARS))
{
rString = (CString)pszMultiByteString;
}
{
delete pszUnicodeString;
}
{
delete pszMultiByteString;
}
}
}
#endif
if (bReadData)
{
// Copied from FileTxt.cpp but adapted to Unicode and then adapted for end-of-line being just '/r'.
nLen = rString.GetLength();
if (nLen > 1 && rString.Mid(nLen-2) == sNEWLINE)
{
rString.GetBufferSetLength(nLen-2);
}
else
{
lpsz = rString.GetBuffer(0);
if (nLen != 0 && (lpsz[nLen-1] == _T('/r') || lpsz[nLen-1] == _T('/n')))
{
rString.GetBufferSetLength(nLen-1);
}
}
}
}
//
// CStdioFileEx::WriteString()
//
// --------------------------------------------------------------------------------------------
// Returns: void
// Parameters: LPCTSTR lpsz
//
// Purpose: Writes string to file either in Unicode or multibyte, depending on whether the caller specified the
// CStdioFileEx::modeWriteUnicode flag. Override of base class function.
// Notes: If writing in Unicode we need to:
// a) Write the Byte-order-mark at the beginning of the file
// b) Write all strings in byte-mode
// - If we were compiled in Unicode, we need to convert Unicode to multibyte if
// we want to write in multibyte
// - If we were compiled in multi-byte, we need to convert multibyte to Unicode if
// we want to write in Unicode.
// Exceptions: None.
//
void CStdioFileEx::WriteString(LPCTSTR lpsz)
{
// If writing Unicode and at the start of the file, need to write byte mark
if (m_nFlags & CStdioFileEx::modeWriteUnicode)
{
// If at position 0, write byte-order mark before writing anything else
if (!m_pStream || GetPosition() == 0)
{
wchar_t cBOM = (wchar_t)nUNICODE_BOM;
CFile::Write(&cBOM, sizeof(wchar_t));
}
}
#ifdef _UNICODE
if (m_nFlags & CStdioFileEx::modeWriteUnicode)
{
// Write in byte mode
CFile::Write(lpsz, lstrlen(lpsz) * sizeof(wchar_t));
}
// Else if we don't want to write Unicode, need to convert
else
{
int nChars = lstrlen(lpsz) + 1; // Why plus 1? Because yes
int nBufferSize = nChars * sizeof(char);
wchar_t* pszUnicodeString = new wchar_t[nChars];
char * pszMultiByteString= new char[nChars];
lstrcpy(pszUnicodeString, lpsz);
if (GetMultiByteStringFromUnicodeString(pszUnicodeString, pszMultiByteString, nBufferSize, GetACP()))
{
// Do standard write
CFile::Write((const void*)pszMultiByteString, lstrlen(lpsz));
}
{
delete [] pszUnicodeString;
delete [] pszMultiByteString;
}
}
// Else if *not* compiled in Unicode
#else
// If writing Unicode, need to convert
if (m_nFlags & CStdioFileEx::modeWriteUnicode)
{
int nChars = lstrlen(lpsz) + 1; // Why plus 1? Because yes
int nBufferSize = nChars * sizeof(wchar_t);
wchar_t* pszUnicodeString = new wchar_t[nChars];
char * pszMultiByteString= new char[nChars];
lstrcpy(pszMultiByteString, lpsz);
{
// Write in byte mode
CFile::Write(pszUnicodeString, lstrlen(lpsz) * sizeof(wchar_t));
}
else
{
ASSERT(false);
}
{
delete [] pszUnicodeString;
delete [] pszMultiByteString;
}
}
// Else if we don't want to write Unicode, no conversion needed
else
{
// Do standard stuff
CStdioFile::WriteString(lpsz);
}
}
{
m_bIsUnicodeText = false;
#ifdef _DEBUG
if (nOpenFlags & CStdioFileEx::modeWriteUnicode)
{
ASSERT(nOpenFlags & CFile::modeWrite || nOpenFlags & CFile::modeReadWrite);
}
#endif
if (nOpenFlags & CFile::typeText && !(m_nFlags & CFile::modeCreate) && !(m_nFlags & CFile::modeWrite ))
{
m_bIsUnicodeText = IsFileUnicode(sFilePath);
if (m_bIsUnicodeText)
{
nOpenFlags ^= CFile::typeText;
nOpenFlags |= CFile::typeBinary;
}
}
}
//
// CStdioFileEx::IsFileUnicode()
//
// --------------------------------------------------------------------------------------------
// Returns: bool
// Parameters: const CString& sFilePath
//
// Purpose: Determines whether a file is Unicode by reading the first character and detecting
// whether it's the Unicode byte marker.
// Notes: None.
// Exceptions: None.
//
/*static*/ bool CStdioFileEx::IsFileUnicode(const CString& sFilePath)
{
CFile file;
bool bIsUnicode = false;
wchar_t cFirstChar;
CFileException exFile;
if (file.Open(sFilePath, CFile::typeBinary | CFile::modeRead, &exFile))
{
// If byte is Unicode byte-order marker, let's say it's Unicode
if (file.Read(&cFirstChar, sizeof(wchar_t)) > 0 && cFirstChar == (wchar_t)nUNICODE_BOM)
{
bIsUnicode = true;
}
}
else
{
// Handle error here if you like
}
}
{
int nCharSize;
unsigned long nByteCount, nCharCount = 0;
{
// Get size of chars in file
nCharSize = m_bIsUnicodeText ? sizeof(wchar_t): sizeof(char);
nByteCount = (unsigned long)GetLength();
if (m_bIsUnicodeText)
{
nByteCount = nByteCount - sizeof(wchar_t);
}
nCharCount = (nByteCount / nCharSize);
}
}
//
// CStdioFileEx::GetUnicodeStringFromMultiByteString()
//
// --------------------------------------------------------------------------------------------
// Returns: bool
// Parameters: char * szMultiByteString (IN) Multi-byte input string
// wchar_t* szUnicodeString (OUT) Unicode outputstring
// short nUnicodeBufferSize (IN) Size of Unicode output buffer
// UINT nCodePage (IN) Code page used to perform conversion
// Default = -1 (Get local code page).
//
// Purpose: Gets a Unicode string from a MultiByte string.
// Notes: None.
// Exceptions: None.
//
bool CStdioFileEx::GetUnicodeStringFromMultiByteString(char * szMultiByteString, wchar_t* szUnicodeString, short nUnicodeBufferSize, UINT nCodePage)
{
bool bOK = true;
int nReturn = 0;
CString sErrorMsg;
if (szUnicodeString && szMultiByteString)
{
// If no code page specified, take default for system
if (nCodePage == -1)
{
nCodePage = GetACP();
}
{
nReturn = MultiByteToWideChar(nCodePage,MB_PRECOMPOSED,szMultiByteString,-1,szUnicodeString,nUnicodeBufferSize);
{
bOK = false;
}
}
catch(...)
{
bOK = false;
}
}
else
{
bOK = false;
}
return bOK;
}
//
// CStdioFileEx::GetMultiByteStringFromUnicodeString()
//
// --------------------------------------------------------------------------------------------
// Returns: BOOL
// Parameters: wchar_t * szUnicodeString (IN) Unicode input string
// char* szMultiByteString (OUT) Multibyte output string
// short nMultiByteBufferSize (IN) Multibyte buffer size
// UINT nCodePage (IN) Code page used to perform conversion
// Default = -1 (Get local code page).
//
// Purpose: Gets a MultiByte string from a Unicode string
// Notes: None.
// Exceptions: None.
//
BOOL CStdioFileEx::GetMultiByteStringFromUnicodeString(wchar_t * szUnicodeString, char* szMultiByteString,
short nMultiByteBufferSize, UINT nCodePage)
{
BOOL bUsedDefChar = FALSE;
BOOL bGotIt = FALSE;
{
// If no code page specified, take default for system
if (nCodePage == -1)
{
nCodePage = GetACP();
}
{
bGotIt = WideCharToMultiByte(nCodePage, WC_COMPOSITECHECK | WC_SEPCHARS,
szUnicodeString,-1, szMultiByteString, nMultiByteBufferSize, sDEFAULT_UNICODE_FILLER_CHAR, &bUsedDefChar);
}
catch(...)
{
TRACE(_T("Controlled exception in WideCharToMultiByte!/n"));
}
}
}
相关推荐
CStdioFileEx(支持ANSI、UNICODE、UNICODE big endian、UTF-8编码的文本读取
/*程序用途:按行读取常见 (包括ANSI、UNICODE、UNICODE big endian、UTF-8)格式的文本文件 */ //核心算法:CStdioFileEx继承自CStdioFile, 覆盖CStdioFile的 BOOL ReadString(CString& rString)方法, // 根据不同文件编码特征,寻找文件回车换行符判断读取行结束,文件结束符判断文件结束 // 检测不同文件编码头部,获取文件类型后调用不同的读取函数 //测试结果:在Windows7 VS2008环境下测试上述四种格式的txt文件通过 //尚未完成:未重载CStdioFile的 virtual LPTSTR ReadString( LPTSTR lpsz, UINT nMax )方法 // 未完成WriteString方法,未在VC UNICODE 环境下的测试
【CStdioFile扩展(支持Ansi、Unicode、Utf-8等文本格式)】
例如:以下是官方博客例子浅析先加入相关的头文件和库文件。提示:以下是本篇文章正文内容,下面案例可供参考提示:这里对文章进行总结:例如:以上就是今天要讲的内容,本文仅仅简单介绍了的函数和方法。
支持UNICODE读写的MFC扩展类 CStdioFileEx 的例子
支持UNICODE读写的MFC扩展类 CStdioFileEx 的例子
CStdioFileEx
<br />class CStdioFileEx : public CStdioFile<br />{<br />public:<br /> BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL )<br /> {<br /> if(CStdioFile::Open(lpszFileName,nOpenFlags,pError))<br /> {<br /> BYTE head[3];<br /
注册表文件(reg)转换成安装文件(inf)
通过将reg文件转换成inf文件,这样就可以使用RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 128 .\xxx.inf进行绿色软件的注册表注册了。
CANoe: Panel的上位机控制方法
本文介绍了不同于IG的报文控制方法,实现了Panel面板的控制模式。
按行读取ANSI、UNICODE 、UNICODE big endian、UTF-8四种文本文
代码简介 1.问题提出 MFC提供的文件类CStdioFile,其中一个函数ReadString实现了文件的按行读取,但是不能满足不同类型的文本文件的按行读取,为了解决这一问题,笔者初步研究了一些编码知识,参考了网上的一些资料,实现了CStdioFile类的扩展类CStdioFileEx,完成了常见文本文件的按行读取(注明:不包括DOC、PDF等其他形式的文档). 在此对网上分享编码经验的网
CCS环境下ADC采样波形的实时可视化调试技巧
本文详细介绍了在CCS环境下实现ADC采样波形实时可视化的调试技巧。通过数组缓存优化、多通道同步显示和波形测量工具的高级用法,帮助开发者高效调试嵌入式系统中的ADC采样问题,提升信号分析的准确性和效率。
VC ANSI环境下按行读取ANSI、UNICODE 、UNICODE big endian、UTF-8四种文本文件
VC ANSI环境下按行读取ANSI、UNICODE 、UNICODE big endian、UTF-8四种文本文件 1.问题提出 MFC提供的文件类CStdioFile,其中一个函数ReadString实现了文件的按行读取,但是不能满足不同类型的文本文件的按行读取,为了解决这一问题,笔者初步研究了一些编码知识,参考了网上的一些资料,实现了CStdioFile类的扩展类CStdioFile
微信小程序获取用户手机号全流程实战(附完整代码)
本文详细介绍了微信小程序获取用户手机号的全流程实战,包括前端授权、后端解密及安全合规要点。通过传统授权按钮和新版手机号快速验证组件两种方案,帮助开发者高效实现功能,并附完整代码示例。重点解析了微信小程序接口的使用技巧和常见问题解决方案,确保数据安全与用户体验。
贝叶斯校准在计算机模型不确定性分析中的实践指南
本文深入探讨了贝叶斯校准在计算机模型不确定性分析中的核心原理与实践方法。通过将模型从“黑箱”转变为“透明箱”,该方法系统量化了参数不确定性、模型结构缺陷及观测误差。文章重点阐释了如何利用高斯过程作为代理模型和模型不足函数,构建完整的贝叶斯校准框架,并提供了从数据准备、模型训练到MCMC推断及不确定性分析的实战指南,最终实现更可靠的预测与决策支持。
将CStdioFile类扩展,读取UNICODE文本文件
最近因为要读取SQL脚本文件,用CStdioFile来读取脚本文件,却在程序调试时读取不了文件。后来看了一下文本文件格式,竟然是UNICODE格式的,原来在导出SQL脚本的时候,选项默认的是UNICODE格式。为了同时支持ANSI和UNICODE两种格式,在Codeproject站点上...
【python】epub的电子书繁体字与简体字转换
中文繁体与简体转换
CstdioFileEx读写UNICODE文本文件
CstdioFileEx读写UNICODE文本文件 弄了好久,都没把TXT文件乱码的问题搞定…… 步骤:1. 到http://www.codeproject.com/下载CstdioFileEx类2. 使用CstdioFileEx示例代码如下: TCHAR FileName[100]; CStdioFi
论文阅读笔记---Deep HDR Video from Sequences with Alternating Exposures
Kalantari N K, Ramamoorthi R. Deep HDR Video from Sequences with Alternating Exposures[C]//Computer Graphics Forum. 2019, 38(2): 193-205. 摘要: 在本文中,提出了一种基于学习的方法来,我们使用两个顺序卷积神经网络(CNN)对整个HDR视频重建过程进...
直流无刷电机FOC控制算法 理论到实践 —— 实践
关于直流无刷电机FOC控制算法的理论章节,本人已经在前两章进行过详细说明,大家可以自行进行阅读,请务必了解过理论之后再来学习如何具体进行实现。直流无刷电机FOC控制算法 理论到实践 —— 理论(一)直流无刷电机FOC控制算法 理论到实践 —— 理论(二)本章节主要讨论采用MCU微控制器如何具体实现直流无刷电机的FOC控制。
将CStdioFile类扩展,读取UNICODE和ANSI文本文件
原文:http://dev.csdn.net/article/38/38303.shtm在此基础上修改了部分代码。 最近因为要读取SQL脚本文件,用CStdioFile来读取脚本文件,却在程序调试时读取不了文件。后来看了一下文本文件格式,竟然是UNICODE格式的,原来在导出SQL脚本的时候,选项默认的是UNICODE格式。为了同时支持ANSI和UNICODE两种格式,在Code
BLDC&PMSM电机控制器硬件设计工程(八)72V 10kW电机控制器原理图工程及库文件
电机控制器基本需求:接口类型;MCU平台;通信接口;供电;信号采集;信号输出;功率;EMC或者认证需求。
UNICODE环境下增强型CStdioFileEx
由于行结尾标识符不同,UNICODE下中文读取用CstdioFile时,经常有乱码出现。有个高手写了个增强型的类 地址在这里
锂电池充电器电路设计 里面有电路
锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。
廖雪峰Python教程[完整版]
廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白
VC 编程ANSI环境下读写Unicode文件和将CStdioFile类扩展,读取UNICODE文本文件
VC 编程ANSI环境下读写Unicode文件没有注意到文件编码的不同会产生这么多的问题,在动手以前查询了很多资料,在本博客中收藏了不少先辈的成果,在这里一并表示致敬! 关于ANSI和Unicode编码的原理在这里也不说了,主要讲下如何读写! 首先确定你的工程是采用的是什么编码环境,默认是ANSI,不同的字符集读写文件的差别也比较大,我这里只在ANSI环境下做的,下一步在探索在Unicode环境下...
visio安装包
Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。
CStdioFile扩展(支持Ansi、Unicode、Utf-8等文本格式)
头文件声明(CStdioFileEx.h): // StdioFileEx.h: interface for the CStdioFileEx class. // // Version 1.1 23 August 2003. Incorporated fixes from Dennis Jeryd. // Version 1.3 19 February 2005. Incorporated fixes from Howard J Oh and some of my own. // Version 1.4
数学建模模型总结
这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!
1036




被折叠的 条评论
为什么被折叠?



