最近在用MFC写一个小的工具,从一个读取一个带特殊标签的文件并将里面的标签替换成对应的数据,然后写入另一个生成文件里。由于输入输出文件是Ansi编码的,程序当中CString类存储格式却是UNICODE编码的,所以不得不解决编码转换的问题。
Ansi转UNICODE
- /***************************/
- /* ansi-unicode conversion */
- /***************************/
- BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
- BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
/***************************/ /* ansi-unicode conversion */ /***************************/ BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; } BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; }
将Ansi编码的文本文件读取存入UNICODE编码的CString
虽然不进行类型转换也可以把读取到的Ansi文本交给CString去处理,但这样CString里面的查找函数是不能正确工作的,因为编码格式不对。所以在做这个操作时,要把编码格式转换一下(本人由于之前没有转换就出现了问题,替换不成功,并且输出文件出现乱码)
以下代码将Ansi文件全部读入并存到成员变量CString m_strTemplate中。代码中动态分配内存,请注意delete。
- BOOL HtmlGenerator::LoadTemplate(CString strPath)
- {
- CFile cfile;
- CFileException ex;
- char *pBuf = NULL;
- TCHAR *pTcBuf = NULL;
- UINT nFileLen = 0;
- // 打开文件
- if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex))
- {
- TCHAR szError[1024];
- ex.GetErrorMessage(szError, 1024);
- TRACE1("Couldn't open source file: %1024s\n", szError);
- return FALSE;
- }
- // 获得文件长度
- nFileLen = (int)cfile.GetLength();
- // 分配内存进行读取
- pBuf = new char[nFileLen + 1];
- pTcBuf = new TCHAR[nFileLen + 1];
- cfile.Read(pBuf, nFileLen);
- pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0'
- // 将Ansi编码转为UNICODE
- AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen);
- // 将读取的模板文件保存到成员变量中
- m_strTemplate = pTcBuf;
- m_strHtmlContent = m_strTemplate;
- // 回收动态分配的内存
- delete [] pBuf;
- delete [] pTcBuf;
- cfile.Close();
- return TRUE;
- }
BOOL HtmlGenerator::LoadTemplate(CString strPath) { CFile cfile; CFileException ex; char *pBuf = NULL; TCHAR *pTcBuf = NULL; UINT nFileLen = 0; // 打开文件 if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE1("Couldn't open source file: %1024s\n", szError); return FALSE; } // 获得文件长度 nFileLen = (int)cfile.GetLength(); // 分配内存进行读取 pBuf = new char[nFileLen + 1]; pTcBuf = new TCHAR[nFileLen + 1]; cfile.Read(pBuf, nFileLen); pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0' // 将Ansi编码转为UNICODE AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen); // 将读取的模板文件保存到成员变量中 m_strTemplate = pTcBuf; m_strHtmlContent = m_strTemplate; // 回收动态分配的内存 delete [] pBuf; delete [] pTcBuf; cfile.Close(); return TRUE; }
UNICODE转Ansi
这个部分可以调用WideCharToMultiByte来完成,但是如果使用CString的话,有一种更简单的解决办法:
- // 将UNICODE转回Ansi编码
- CStringA strAnsi(m_strHtmlContent.GetBuffer(0));
- cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength());
// 将UNICODE转回Ansi编码 CStringA strAnsi(m_strHtmlContent.GetBuffer(0)); cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength());
上述代码中
CStringA strAnsi(m_strHtmlContent.GetBuffer(0));将以UNICODE编码的CString m_strHtmlContent转成了ANSI格式编码的CStringA strAnsi,要获取Ansi格式编码的字符串,只需要调用strAnsi.GetBuffer()就可以了。
标签替换工具源码
HtmlGenerator.h
- #pragma once
- class HtmlGenerator
- {
- public:
- HtmlGenerator(void);
- ~HtmlGenerator(void);
- BOOL LoadTemplate(CString strPath); // 装载HTML模板
- BOOL WriteHtml(CString strPath); // 将处理好的HTML写入文件
- int ReplaceTag(CString strTag, CString strValue); // 替换标签,返回值表示替换掉的标签个数
- void Restore(void){m_strHtmlContent = m_strTemplate;} // 将修改过的HTML恢复成与模板相同
- private:
- CString m_strTemplate;
- CString m_strHtmlContent;
- };
- BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen);
- BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen);
#pragma once class HtmlGenerator { public: HtmlGenerator(void); ~HtmlGenerator(void); BOOL LoadTemplate(CString strPath); // 装载HTML模板 BOOL WriteHtml(CString strPath); // 将处理好的HTML写入文件 int ReplaceTag(CString strTag, CString strValue); // 替换标签,返回值表示替换掉的标签个数 void Restore(void){m_strHtmlContent = m_strTemplate;} // 将修改过的HTML恢复成与模板相同 private: CString m_strTemplate; CString m_strHtmlContent; }; BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen); BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen);
HtmlGenerator.cpp
- #include "StdAfx.h"
- #include "HtmlGenerator.h"
- HtmlGenerator::HtmlGenerator(void)
- {
- }
- HtmlGenerator::~HtmlGenerator(void)
- {
- }
- BOOL HtmlGenerator::LoadTemplate(CString strPath)
- {
- CFile cfile;
- CFileException ex;
- char *pBuf = NULL;
- TCHAR *pTcBuf = NULL;
- UINT nFileLen = 0;
- // 打开文件
- if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex))
- {
- TCHAR szError[1024];
- ex.GetErrorMessage(szError, 1024);
- TRACE1("Couldn't open source file: %1024s\n", szError);
- return FALSE;
- }
- // 获得文件长度
- nFileLen = (int)cfile.GetLength();
- // 分配内存进行读取
- pBuf = new char[nFileLen + 1];
- pTcBuf = new TCHAR[nFileLen + 1];
- cfile.Read(pBuf, nFileLen);
- pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0'
- // 将Ansi编码转为UNICODE
- AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen);
- // 将读取的模板文件保存到成员变量中
- m_strTemplate = pTcBuf;
- m_strHtmlContent = m_strTemplate;
- // 回收动态分配的内存
- delete [] pBuf;
- delete [] pTcBuf;
- cfile.Close();
- return TRUE;
- }
- BOOL HtmlGenerator::WriteHtml(CString strPath)
- {
- CFile cfile;
- CFileException ex;
- if (!cfile.Open(strPath, CFile::modeCreate | CFile::modeReadWrite, &ex))
- {
- TCHAR szError[1024];
- ex.GetErrorMessage(szError, 1024);
- TRACE1("Couldn't open dest file: %1024s\n", szError);
- return FALSE;
- }
- // 将UNICODE转回Ansi编码
- CStringA strAnsi(m_strHtmlContent.GetBuffer(0));
- cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength());
- cfile.Flush();
- cfile.Close();
- return TRUE;
- }
- int HtmlGenerator::ReplaceTag(CString strTag, CString strValue)
- {
- return m_strHtmlContent.Replace(strTag, strValue);
- }
- /***************************/
- /* ansi-unicode conversion */
- /***************************/
- BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
- BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
#include "StdAfx.h" #include "HtmlGenerator.h" HtmlGenerator::HtmlGenerator(void) { } HtmlGenerator::~HtmlGenerator(void) { } BOOL HtmlGenerator::LoadTemplate(CString strPath) { CFile cfile; CFileException ex; char *pBuf = NULL; TCHAR *pTcBuf = NULL; UINT nFileLen = 0; // 打开文件 if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE1("Couldn't open source file: %1024s\n", szError); return FALSE; } // 获得文件长度 nFileLen = (int)cfile.GetLength(); // 分配内存进行读取 pBuf = new char[nFileLen + 1]; pTcBuf = new TCHAR[nFileLen + 1]; cfile.Read(pBuf, nFileLen); pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0' // 将Ansi编码转为UNICODE AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen); // 将读取的模板文件保存到成员变量中 m_strTemplate = pTcBuf; m_strHtmlContent = m_strTemplate; // 回收动态分配的内存 delete [] pBuf; delete [] pTcBuf; cfile.Close(); return TRUE; } BOOL HtmlGenerator::WriteHtml(CString strPath) { CFile cfile; CFileException ex; if (!cfile.Open(strPath, CFile::modeCreate | CFile::modeReadWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE1("Couldn't open dest file: %1024s\n", szError); return FALSE; } // 将UNICODE转回Ansi编码 CStringA strAnsi(m_strHtmlContent.GetBuffer(0)); cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength()); cfile.Flush(); cfile.Close(); return TRUE; } int HtmlGenerator::ReplaceTag(CString strTag, CString strValue) { return m_strHtmlContent.Replace(strTag, strValue); } /***************************/ /* ansi-unicode conversion */ /***************************/ BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; } BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; }
最近在用MFC写一个小的工具,从一个读取一个带特殊标签的文件并将里面的标签替换成对应的数据,然后写入另一个生成文件里。由于输入输出文件是Ansi编码的,程序当中CString类存储格式却是UNICODE编码的,所以不得不解决编码转换的问题。
Ansi转UNICODE
- /***************************/
- /* ansi-unicode conversion */
- /***************************/
- BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
- BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen)
- {
- /* locals */
- INT lv_Len;
- // do NOT decrease maxlen for the eos
- if (in_MaxLen <= 0)
- return FALSE;
- // let windows find out the meaning of ansi
- // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small.
- // - if SrcLen is specified then no eos is added
- // - if (SrcLen+1) is specified then the eos IS added
- lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen);
- // validate
- if (lv_Len < 0)
- lv_Len = 0;
- // ensure eos, watch out for a full buffersize
- // - if the buffer is full without an eos then clear the output like MBTWC does
- // in case of too small outputbuffer
- // - unfortunately there is no way to let MBTWC return shortened strings,
- // if the outputbuffer is too small then it fails completely
- if (lv_Len < in_MaxLen)
- out_Dst[lv_Len] = 0;
- else if (out_Dst[in_MaxLen-1])
- out_Dst[0] = 0;
- // done
- return TRUE;
- }
/***************************/ /* ansi-unicode conversion */ /***************************/ BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, -1, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; } BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen) { /* locals */ INT lv_Len; // do NOT decrease maxlen for the eos if (in_MaxLen <= 0) return FALSE; // let windows find out the meaning of ansi // - the SrcLen=-1 triggers MBTWC to add a eos to Dst and fails if MaxLen is too small. // - if SrcLen is specified then no eos is added // - if (SrcLen+1) is specified then the eos IS added lv_Len = MultiByteToWideChar(CP_ACP, 0, in_Src, in_SrcLen, out_Dst, in_MaxLen); // validate if (lv_Len < 0) lv_Len = 0; // ensure eos, watch out for a full buffersize // - if the buffer is full without an eos then clear the output like MBTWC does // in case of too small outputbuffer // - unfortunately there is no way to let MBTWC return shortened strings, // if the outputbuffer is too small then it fails completely if (lv_Len < in_MaxLen) out_Dst[lv_Len] = 0; else if (out_Dst[in_MaxLen-1]) out_Dst[0] = 0; // done return TRUE; }
将Ansi编码的文本文件读取存入UNICODE编码的CString
虽然不进行类型转换也可以把读取到的Ansi文本交给CString去处理,但这样CString里面的查找函数是不能正确工作的,因为编码格式不对。所以在做这个操作时,要把编码格式转换一下(本人由于之前没有转换就出现了问题,替换不成功,并且输出文件出现乱码)
以下代码将Ansi文件全部读入并存到成员变量CString m_strTemplate中。代码中动态分配内存,请注意delete。
- BOOL HtmlGenerator::LoadTemplate(CString strPath)
- {
- CFile cfile;
- CFileException ex;
- char *pBuf = NULL;
- TCHAR *pTcBuf = NULL;
- UINT nFileLen = 0;
- // 打开文件
- if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex))
- {
- TCHAR szError[1024];
- ex.GetErrorMessage(szError, 1024);
- TRACE1("Couldn't open source file: %1024s\n", szError);
- return FALSE;
- }
- // 获得文件长度
- nFileLen = (int)cfile.GetLength();
- // 分配内存进行读取
- pBuf = new char[nFileLen + 1];
- pTcBuf = new TCHAR[nFileLen + 1];
- cfile.Read(pBuf, nFileLen);
- pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0'
- // 将Ansi编码转为UNICODE
- AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen);
- // 将读取的模板文件保存到成员变量中
- m_strTemplate = pTcBuf;
- m_strHtmlContent = m_strTemplate;
- // 回收动态分配的内存
- delete [] pBuf;
- delete [] pTcBuf;
- cfile.Close();
- return TRUE;
- }
BOOL HtmlGenerator::LoadTemplate(CString strPath) { CFile cfile; CFileException ex; char *pBuf = NULL; TCHAR *pTcBuf = NULL; UINT nFileLen = 0; // 打开文件 if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE1("Couldn't open source file: %1024s\n", szError); return FALSE; } // 获得文件长度 nFileLen = (int)cfile.GetLength(); // 分配内存进行读取 pBuf = new char[nFileLen + 1]; pTcBuf = new TCHAR[nFileLen + 1]; cfile.Read(pBuf, nFileLen); pBuf[nFileLen] = '\0'; // 将字符串结尾标记为'\0' // 将Ansi编码转为UNICODE AnsiToUnicode16L(pBuf, nFileLen, pTcBuf, nFileLen); // 将读取的模板文件保存到成员变量中 m_strTemplate = pTcBuf; m_strHtmlContent = m_strTemplate; // 回收动态分配的内存 delete [] pBuf; delete [] pTcBuf; cfile.Close(); return TRUE; }
UNICODE转Ansi
这个部分可以调用WideCharToMultiByte来完成,但是如果使用CString的话,有一种更简单的解决办法:
- // 将UNICODE转回Ansi编码
- CStringA strAnsi(m_strHtmlContent.GetBuffer(0));
- cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength());
// 将UNICODE转回Ansi编码 CStringA strAnsi(m_strHtmlContent.GetBuffer(0)); cfile.Write(strAnsi.GetBuffer(), strAnsi.GetLength());
上述代码中
CStringA strAnsi(m_strHtmlContent.GetBuffer(0));将以UNICODE编码的CString m_strHtmlContent转成了ANSI格式编码的CStringA strAnsi,要获取Ansi格式编码的字符串,只需要调用strAnsi.GetBuffer()就可以了。
标签替换工具源码
HtmlGenerator.h
- #pragma once
- class HtmlGenerator
- {
- public:
- HtmlGenerator(void);
- ~HtmlGenerator(void);
- BOOL LoadTemplate(CString strPath); // 装载HTML模板
- BOOL WriteHtml(CString strPath); // 将处理好的HTML写入文件
- int ReplaceTag(CString strTag, CString strValue); // 替换标签,返回值表示替换掉的标签个数
- void Restore(void){m_strHtmlContent = m_strTemplate;} // 将修改过的HTML恢复成与模板相同
- private:
- CString m_strTemplate;
- CString m_strHtmlContent;
- };
- BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen);
- BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen);
#pragma once class HtmlGenerator { public: HtmlGenerator(void); ~HtmlGenerator(void); BOOL LoadTemplate(CString strPath); // 装载HTML模板 BOOL WriteHtml(CString strPath); // 将处理好的HTML写入文件 int ReplaceTag(CString strTag, CString strValue); // 替换标签,返回值表示替换掉的标签个数 void Restore(void){m_strHtmlContent = m_strTemplate;} // 将修改过的HTML恢复成与模板相同 private: CString m_strTemplate; CString m_strHtmlContent; }; BOOL AnsiToUnicode16(CHAR *in_Src, WCHAR *out_Dst, INT in_MaxLen); BOOL AnsiToUnicode16L(CHAR *in_Src, INT in_SrcLen, WCHAR *out_Dst, INT in_MaxLen);
HtmlGenerator.cpp
- #include "StdAfx.h"
- #include "HtmlGenerator.h"
- HtmlGenerator::HtmlGenerator(void)
- {
- }
- HtmlGenerator::~HtmlGenerator(void)
- {
- }
- BOOL HtmlGenerator::LoadTemplate(CString strPath)
- {
- CFile cfile;
- CFileException ex;
- char *pBuf = NULL;
- TCHAR *pTcBuf = NULL;
- UINT nFileLen = 0;
- // 打开文件
- if (!cfile.Open(strPath, CFile::modeRead | CFile::shareDenyWrite, &ex))
- {
- TCHAR szError[1024];
- ex.GetErrorMessage(szError, 1024);
- TRACE1("Couldn't open source file: %1024s\n", szError);
- return FALSE;