UNICODE 问题!!! UP 有分!!

ghosert 2003-04-15 08:00:30
CMapLayer::CMapLayer(CDemo2Doc* pDoc, CArchive& ar)
{
CString str, str1;
int i, j, k;
CMapObject* object;

m_pDoc = pDoc;
m_nMaxPoint = 0;
m_bCanDraw = true;

while ( ar.ReadString(str) )//一行一行的读,田加的
{
str1=GetFirstPara(str);
if(str1=="Region")
{//区域图元
j=GetSecondPara(str);//读多边形个数
for(i=0;i<j;i++)
{//创建多边形对象
ar.ReadString(str);
k = atoi(LPCTSTR(str));//读当前多边形端点数
object = new CMapRegion(pDoc, ar, k);
m_aObject.Add(object);
m_nMaxPoint = m_nMaxPoint > object->m_nMaxPoint ? m_nMaxPoint : object->m_nMaxPoint;
}
}
else if(str1=="Pline")
{//线图元
j=GetSecondPara(str);//读折线端点数
object = new CMapPLine(pDoc, ar, j);
m_aObject.Add(object);
m_nMaxPoint = m_nMaxPoint > object->m_nMaxPoint ? m_nMaxPoint : object->m_nMaxPoint;
}
else if(str1=="Text")
{
object = new CMapText(pDoc, ar);
m_aObject.Add(object);
m_nMaxPoint = m_nMaxPoint > object->m_nMaxPoint ? m_nMaxPoint : object->m_nMaxPoint;
}
}
}


本想通过行读while ( ar.ReadString(str) ) 来取到文本文件中的每一行,然后判断关键字if(str1=="Region"),if(str1=="Pline"),if(str1=="Text") 来进行绘图,但现在取出的STR是乱码,后来才发现,STR内读取文本是按双字节进行的,每次读两个字符,因此产生了类似方块字的乱码,有什么解决方案吗?该程序由VC版本移植.VC下正确.
...全文
72 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghosert 2003-04-17
  • 打赏
  • 举报
回复
springmaster(奋斗先锋) :

还是你说的对,不是程序的角度考虑问题,而是从源文件的角度考虑问题.进行转化后可以了.太感谢了。送分!!!!
西门豆豆 2003-04-16
  • 打赏
  • 举报
回复
用UltraEdit打开该文件,在File菜单里有一个conversion选项,如果Unicode to ASCII码转化不是反白显示,则证明你的程序是Unicode 格式,然后将其转化即可!
acaicb 2003-04-16
  • 打赏
  • 举报
回复
我也碰到这种问题,
Unicode格式文件以0xfffe开头

MS说用MultiByteToWideChar进行转换,但我试了不行,不知为何?

去看看我的Question
lichun3000 2003-04-16
  • 打赏
  • 举报
回复
up
ghosert 2003-04-15
  • 打赏
  • 举报
回复
所有判断的地方改双字节,并未改变STR变量乱玛的状况,问题依旧存在,贴上CArchive类
BOOL CArchive::ReadString(CString& rString)
{
rString = &afxChNil; // empty string without deallocating
const int nMaxSize = 128;
LPTSTR lpsz = rString.GetBuffer(nMaxSize);
LPTSTR lpszResult;
int nLen;
for (;;)
{
lpszResult = ReadString(lpsz, (UINT)-nMaxSize); // store the newline
rString.ReleaseBuffer();

// if string is read completely or EOF
if (lpszResult == NULL ||
(nLen = lstrlen(lpsz)) < nMaxSize ||
lpsz[nLen-1] == '\n')
{
break;
}

nLen = rString.GetLength();
lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
}

// remove '\n' from end of string if present
lpsz = rString.GetBuffer(0);
nLen = rString.GetLength();
if (nLen != 0 && lpsz[nLen-1] == '\n')
rString.GetBufferSetLength(nLen-1);

return lpszResult != NULL;
}


LPTSTR CArchive::ReadString(LPTSTR lpsz, UINT nMax)
{
// if nMax is negative (such a large number doesn't make sense given today's
// 2gb address space), then assume it to mean "keep the newline".
int nStop = (int)nMax < 0 ? -(int)nMax : (int)nMax;
ASSERT(AfxIsValidAddress(lpsz, (nStop+1) * sizeof(TCHAR)));

_TUCHAR ch;
int nRead = 0;

TRY
{
while (nRead < nStop)
{
*this >> ch;

// stop and end-of-line (trailing '\n' is ignored)
if (ch == '\n' || ch == '\r')
{
if (ch == '\r')
*this >> ch;
// store the newline when called with negative nMax
if ((int)nMax != nStop)
lpsz[nRead++] = ch;
break;
}
lpsz[nRead++] = ch;
}
}
CATCH(CArchiveException, e)
{
if (e->m_cause == CArchiveException::endOfFile)
{
DELETE_EXCEPTION(e);
if (nRead == 0)
return NULL;
}
else
{
THROW_LAST();
}
}
END_CATCH

lpsz[nRead] = '\0';
return lpsz;
}


问题就在里变量 ch 是按双字节取出文本内容. 而在VC 里是按单字节,奇怪的是按双字节取出后,判断是否换行if (ch == '\n' || ch == '\r')却又用单字节. 所以该判断事实无效.
而该段代码已经是MFC类库代码,不便自行修改.残念…… 愿高手仗义相助。本人毕业设计,命系于此!!!
西门豆豆 2003-04-15
  • 打赏
  • 举报
回复
把字符串常量前都加上L或_T(),或TEXT()
sunwhite 2003-04-15
  • 打赏
  • 举报
回复
看不出有什么问题,你可否把CArchive类的代码贴上来,我帮你分析分析。
aawolf 2003-04-15
  • 打赏
  • 举报
回复
在EVC下CString也是双字节的,你把所有判断的地方都改成双字节不就可以了吗?
if(str1==L"Pline"),
dizzo 2003-04-15
  • 打赏
  • 举报
回复
gz
ghosert 2003-04-15
  • 打赏
  • 举报
回复
如何知道文本是否按 UNICODE 编码存呢,如果是,如何改成ASC? 另外据我所知,从PC 传到PDA上的文本都被转换成了UNICODE.
aawolf 2003-04-15
  • 打赏
  • 举报
回复
你的文本文件是按Unicode编码存的吗?
aawolf 2003-04-15
  • 打赏
  • 举报
回复
GetFirstPara这是个什么函数?
ghosert 2003-04-15
  • 打赏
  • 举报
回复
你这样写当然是YES,问题在这里。
while ( ar.ReadString(str) )//一行一行的读,田加的
{
str1=GetFirstPara(str);
....
}

STR在这里行读以后被赋的值已经是乱码了,GetFirstPara 取的是该行的首单词.当然还是乱吗, 再往下判断已经没有意义了,不是 L _T 的问题. 我也TRY 过,按 while ( ar.ReadString(_T(str) ) )不过报错了. 问题就是为什么读取硬盘上文本内容的时候是按双字节读取而不是单字节,怎么样让它按单字节读取,从而避免乱吗。
aawolf 2003-04-15
  • 打赏
  • 举报
回复
void CTestView::OnButton1()
{
WCHAR * str=L"hello";

if(str[0]=='h')
AfxMessageBox(TEXT("yes"));
else
AfxMessageBox(TEXT("no"));
}

我写了段代码,这段代码返回yes
lijiuhua0721 2003-04-15
  • 打赏
  • 举报
回复
up

19,504

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧