fgets 读取中文乱码?

FILE *fp = NULL;
fp = fopen("rr.ini", "r");
char buf[128];
if (fp == NULL)
{
return FALSE;
}
int flag = 0;
while (!feof(fp))
{
flag++;
memset(buf,0,128);
fgets(buf, 128, fp);
CString strPath = Char2WChar(DelEnter(buf));
CString strReg("[reg]");
CString strRun("[run]");
CString strName("[name]");
if (strPath == strReg)
{
memset(buf, 0, 128);
fgets(buf, 128, fp);
strPath = Char2WChar(DelEnter(buf));

if (wcsncmp(strPath.Mid(0,7),_T("regname="),7) == 0)
{
strPath = strPath.Mid(8,strPath.GetLength());
if (strPath.GetLength() > 1)
{
m_ReadIniRegList=strPath;
}
m_ReadIniRegList=(L"null");
}
}
else
{
if (1 == flag)
{
m_ReadIniRegList=L"null";
}
}
if (strPath == strRun)
{
memset(buf, 0, 128);
fgets(buf, 128, fp);
strPath = Char2WChar(DelEnter(buf));
if (wcsncmp(strPath.Mid(0,6),_T("runexe="), 6) == 0)
{
strPath = strPath.Mid(7,strPath.GetLength());
m_ReadIniRunList=strPath;
}
else
{
m_ReadIniRunList = L"----";
}
}
if (strPath == strName)
{
memset(buf, '\0', 128);
fgets(buf, 128, fp); //此处读取的中文就会出现乱码(有的不会出现)
strPath = Char2WChar(DelEnter(buf));

if (wcsncmp(strPath.Mid(0,4),_T("name="), 4) == 0)
{
strPath = strPath.Mid(5,strPath.GetLength());
m_ReadIniNameList = strPath;
break;
}
else
{
m_ReadIniNameList = L"----";
}
}

}
fclose(fp);
...全文
1663 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
roony 2012-08-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 5 楼 tuoerr 的回复:
nnd找到原因了 原来文件的编码格式是utf8的


``````````````````````````````````````````````
[/Quote]`````````
sailing0123 2009-01-07
  • 打赏
  • 举报
回复
楼上说的没错,
注意别把一个中文字分开了
  • 打赏
  • 举报
回复
[reg]
读出来的是 “锘縖reg]”
太乙 2009-01-07
  • 打赏
  • 举报
回复
fgetws

用宽字符试试!
lsd1025 2009-01-07
  • 打赏
  • 举报
回复
可能中文是双字节保存一个字符,
128刚好把某个字符截了,所以不完整出现乱码!
SinSoul 2009-01-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tuoerr 的回复:]
nnd找到原因了 原来文件的编码格式是utf8的
[/Quote]

``````````````````````````````````````````````
  • 打赏
  • 举报
回复
nnd找到原因了 原来文件的编码格式是utf8的

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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