VC修改CStdioFile类, 兼容一行一行读写Unicode的和非Unicode的log

有些人写Unicode的log文件时, 不知道在文件前加# define nUNICODE_BOM      0xFEFF 标志位。 当设置在Unicode环境下的VC代码中,使用MFC的CStdioFile类是无法正确调用ReadString()函数获取. 所有需要重写一个CStdioFileEx类.

 

FileEx.h文件:

#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.
	// 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:  Determin
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值