您的位置:首页 > 其它

读取本地UTF-8文件带有换行的中文文本

2009-02-19 01:07 405 查看
代码片段://////////////////////////////////////////////////////////////////////////////

void LoadingMenus(const TDesC& aFilePath)
{
RFs fs;
RFile file;
User::LeaveIfError(fs.Connect());
TInt err=file.Open(fs,aFilePath,EFileRead);
// if(err=KErrNone)
{
TInt nsize;
file.Size(nsize);
HBufC8* hBuf8=HBufC8::NewLC(nsize-3);
TPtr8 ptr=hBuf8->Des();
file.Read(3,ptr,nsize-3);//去掉windows系统在UTF-8编码中加入的3个EF BB BF 字节
_LIT8(KNewLine,"/r/n"); //回车换行

TBuf<20> buf16;
TInt pos1=0;
TInt pos2=0;
TBuf8<420> ibuf8;
ibuf8.Copy(ptr);
TBool isOut=EFalse;
while(pos1<ptr.Length())
{
pos2=ibuf8.Find(KNewLine);
if(pos2==KErrNotFound)
{
ibuf8=ibuf8.Mid(0,ptr.Length()-pos1);
isOut=ETrue;
}
else
ibuf8=ibuf8.Mid(0,pos2);
CnvUtfConverter::ConvertToUnicodeFromUtf8(buf16,ibuf8);
menus.Append(buf16);
pos1+=(pos2+2);
ibuf8=ptr.Mid(pos1);
if(isOut)
break;
}
CleanupStack::PopAndDestroy(hBuf8);
}
file.Close();
fs.Close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐