最近因为要读取SQL脚本文件,用CStdioFile来读取脚本文件,却在程序调试时读取不了文件。 后来看了一下文本文件格式,竟然是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
class CStdioFileEx: public CStdioFile { public: CStdioFileEx(); CStdioFileEx( LPCTSTR lpszFileName, UINT nOpenFlags );
virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL ); virtual BOOL ReadString(CString& rString); virtual void WriteString( LPCTSTR lpsz ); bool IsFileUnicodeText() { return m_bIsUnicodeText; } unsigned long GetCharCount();
// Additional flag to allow Unicode text writing static const UINT modeWriteUnicode;
// static utility functions
// -------------------------------------------------------------------------------------------- // // 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.
复制本页网址和标题,发送给你QQ/Msn的好友一起分享
上一篇:Flash V2组件初探
下一篇:通过middlebox实施P2P通讯三[传]