CStdioFile读的中文是乱码,怎么解决?

shuizhiyun 2008-10-31 02:08:53
	CStdioFile infile;
if (!infile.Open(L"interpreter.txt", CStdioFile::modeRead))
{
AfxMessageBox(L"can not open file");
}

CString str;
while (infile.ReadString(str))

AfxMessageBox(str);

infile.Close();

我把interpreter.txt用UNICODE和ASCII保存都不行。
消息框中显示的是乱码,达人告诉我,谢谢了
...全文
1663 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
kobeyuanlei 2012-09-02
  • 打赏
  • 举报
回复
顶,受教了,感谢大家!!
danuside 2011-05-24
  • 打赏
  • 举报
回复
12楼的兄弟真是厉害!学会了!谢谢!十分感谢!万分感谢!
Beginer09 2010-09-07
  • 打赏
  • 举报
回复
学会了
多谢了
DavidHsing 2009-04-12
  • 打赏
  • 举报
回复
ANSI、UNICODE、UTF-8 文件编码转换
http://blog.csdn.net/DavidHsing/archive/2008/11/17/3317841.aspx
clever101 2009-04-12
  • 打赏
  • 举报
回复
学习了如何读有中文的文件了。
funnymfc 2009-03-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 funnymfc 的回复:]
unicode下请用
C/C++ codechar*old_locale=_strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE,"chs");//设定<ctpye.h>中字符处理方式
处理完后
C/C++ codesetlocale( LC_CTYPE, old_locale );
free( old_locale );//还原区域设定
[/Quote]
需要包含头文件:
#include <locale.h>
shuizhiyun 2009-03-12
  • 打赏
  • 举报
回复
谢谢,楼上的正解
suanday_sunny 2009-03-06
  • 打赏
  • 举报
回复
mark
funnymfc 2009-03-06
  • 打赏
  • 举报
回复
晕,去年的问题。
funnymfc 2009-03-06
  • 打赏
  • 举报
回复
unicode下请用

char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE, "chs" );//设定<ctpye.h>中字符处理方式

处理完后

setlocale( LC_CTYPE, old_locale );
free( old_locale );//还原区域设定
blz_wowar 2009-03-06
  • 打赏
  • 举报
回复
我也mark一下好了,对编码问题我也有遇见过,而且也感兴趣~
yuhudie203 2008-11-04
  • 打赏
  • 举报
回复
mark
shuizhiyun 2008-11-04
  • 打赏
  • 举报
回复
各位把自己的工程设为UNICODE格式的,再读一下看看是不是会出现这种情况?
shuizhiyun 2008-11-03
  • 打赏
  • 举报
回复
问题不像楼上想的那么简单,我的数据结构定义如下:
typedef pair<int, wstring> worditem;//
typedef vector<worditem> worditems;
typedef pair<int, worditems*> field;//first of the pair is not wstring
typedef vector<field> fields;
typedef pair<int, fields*> msg;
typedef vector<msg> msgs;
typedef pair<int, msgs*> channel;
typedef vector<channel> channels;

我会把这个数据结构传给一个函数处理,那个处理函数已经写好了,函数中有大量的处理字符串的操作,我不想再改了,量不是一般的大。

我现在想作的是:
一行行地读取一个文件,再把读取的每一行交给ProcessLine()这个函数处理。
下面是按1楼的思想改的,还是不行,一楼的方法在文件只有几行的情况下没问题,文件大了就出错了。
bool CInterpreter::ProcessLine(CString str)
{
AfxMessageBox(str);
int value;
wchar_t means[256]={L'\0'};

#ifdef _DEBUG
wcout << L"\t\t" << str << endl;
#endif
// AfxMessageBox(str.c_str());
// _stscanf(str.c_str(), L"%*s%d%s", &value, means);
if (str[0] == L';')
{
if (str[1] == L';')
{
if (str[2] == L';')
{
if (str.GetLength() > 3)
{
//信道标志开始
if (m_pMsgs == NULL)
{
m_pMsgs = new msgs;
_stscanf((LPCTSTR)str, L"%*s%d", &value);
m_channels.push_back(make_pair(value, m_pMsgs));
}
}
else
{
//信道标志结束
/*m_channels.push_back(make_pair(value, m_pMsgs));
m_pMsgs = new msgs;*/
m_pMsgs = NULL;
}
}
else
{
if (str.GetLength() > 2)
{
//消息标志开始
m_pFields = new fields;
_stscanf((LPCTSTR)str, L"%*s%d", &value);
m_pMsgs->push_back(make_pair(value, m_pFields));
}
else
{
//消息标志结束
m_pFields = NULL;
}
}
}
else
{
if (str.GetLength() > 1)
{
//域标志开始
m_pWorditems = new worditems;
_stscanf((LPCTSTR)str, L"%*s%d", &value);
m_pFields->push_back(make_pair(value, m_pWorditems));
}
else
{
//域标志结束
m_pWorditems = NULL;
}
}
}
else
{
//域的具体值对应的具体解释
// m_pFields->push_back(make_pair(value, wstring(means)));
//int i = _stscanf(str.c_str(), L"%d", &value);
//wcout << i << L" " << str.substr(str.find(L";")) << endl;
//_stscanf(str.c_str(), L"%d%s", &value, means);
value = _wtoi((LPCTSTR)str);
m_pWorditems->push_back(make_pair(value, wstring((LPCTSTR)str.Mid(str.Find(L";") + 1))));
}
// wcout << L"value= " << value << L" means=" << &means[0] << endl;
// cin.clear();

return true;
}
bool CInterpreter::Configure(wstring strFile)
{
// wifstream infile(L"E:\\GotaBrowser\\release\\interpreter.txt", wios::in);
CStdioFile infile;

if (!infile.Open(L"E:\\GotaBrowser\\release\\interpreter.txt", CStdioFile::modeRead))
{
#ifdef _DEBUG
std::wcerr << L"wrong, can not open the interpreter.txt file, pls check it." << std::endl;
#endif
AfxMessageBox(L"打开interpreter.txt文件失败");
return false;
}
/*
m_pMsgs = new msgs;
m_pFields = new fields;
m_pWorditems = new worditems;
*/
/* wstring str;
while (getline(infile, str))//, L"\n"))
{
ProcessLine(str);
}*/

CString str;
infile.Read(str.GetBufferSetLength(infile.GetLength()/2), infile.GetLength());
AfxMessageBox(str);

int cur, last = 0;
while ( (cur = str.Find(L"\r\n", last)) != -1)
{
ProcessLine(str.Mid(last, cur - last));
last = cur + 2;
}

ProcessLine(str.Mid(last));

infile.Close();

return true;
}


各位帮帮忙
e01620230 2008-11-03
  • 打赏
  • 举报
回复
AfxMessageBox(str);
这个显示的问题,而你用ReadString读出来的都是没有问题的,你用WINHEX仔细看看读出的都是它的16进制值,比如UNICODE的跳过他前面的0xFFFE开始后汉字是2字节一个,那读入怎么样的发送过去的也怎么样,你反正是用来交互的那在接收端进行处理就可以了。
一条晚起的虫 2008-11-03
  • 打赏
  • 举报
回复
读的代码没有问题,能否贴出写的代码?
lwx300 2008-11-03
  • 打赏
  • 举报
回复
如果文件是UNICODE的,要先跳过文件前的2个字节,然后才开始ReadString。
shuizhiyun 2008-11-03
  • 打赏
  • 举报
回复
楼上的再试试用UNICODE显示的是乱码,我要用UNICODE,读取的数据要与COM交互,我不想它与MBCS转来转去:
void CMfctestDlg::OnButton1() 
{
CStdioFile infile;
if (!infile.Open(L"C:\\x.txt", CStdioFile::modeRead))
{
AfxMessageBox(L"can not open file.");
return;
}

CString str;
while (infile.ReadString(str))
{
AfxMessageBox(str);
}

infile.Close();
}
hityct1 2008-11-01
  • 打赏
  • 举报
回复
用的什么编译器?vc6.0这样是没问题的:

CStdioFile infile;
if (!infile.Open("interpreter.txt", CStdioFile::modeRead))
{
AfxMessageBox("can not open file");
}

CString str;
while (infile.ReadString(str))

AfxMessageBox(str);

infile.Close();


shailen126 2008-10-31
  • 打赏
  • 举报
回复
infile.Read(str.GetBufferSetLength(infile.GetLength()),infile.GetLength());//试试

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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