在vs2008环境下,
void CFileView::OnFileWrite()
{
// TODO: 在此添加命令处理程序代码
FILE *pFile=fopen("1.txt","w");
fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
fclose(pFile);
}void CFileView::OnFileRead()
{
// TODO: 在此添加命令处理程序代码
FILE *pFile=fopen("1.txt","r");
char *pBuf;
fseek(pFile,0,SEEK_END);
int len=ftell(pFile);
pBuf=new char[len+1];
fread(pBuf,1,len,pFile);
pBuf[len]=0;
fclose(pFile);
MessageBox(pBuf);
}
messagebox输出的怎么是汉字乱码呢?如何修改?mfcfile

解决方案 »

  1.   

    估计你代码是UNICODE的, 使用::MessageBoxA
      

  2.   

    你的"1.txt" 根本不是 UNICODE 的,
    你用 ‘记事本’ 打开 看看, 
    然后 另存为 UNCODE 的
    再运行程序
      

  3.   

    我用了unicode,再用通用的messagebox没问题吧?
      

  4.   


    改了1.txt格式,还是不行,代码都改成unicode的还是不行:void CFileView::OnFileWrite()
    {
    // TODO: 在此添加命令处理程序代码
    FILE *pFile=fopen("1.txt","w");
    fwrite(_T("http://www.sunxin.org"),2,strlen("http://www.sunxin.org"),pFile);
    fclose(pFile);

    }void CFileView::OnFileRead()
    {
    FILE *pFile=fopen("1.txt","r");
    TCHAR *pBuf;
    fseek(pFile,0,SEEK_END);
    int len=ftell(pFile);
    pBuf=new TCHAR[len+1];
    fread(pBuf,1,len,pFile);
    pBuf[len]=0;
    fclose(pFile);
    MessageBox(pBuf);
    }
      

  5.   

    同样遇到过 http://bbs.csdn.net/topics/320228998这个看了么?太多
      

  6.   

    fseek(pFile,0,SEEK_END);  你这里把文件指针移动到了最后, 后面却没看见移回来, 第一个悲剧.pBuf=new TCHAR[len+1]; 光是申请空间, 但未初始化, 第二悲剧.所以就乱码了.
      

  7.   


    我用的是c的库函数,而且我把unicode改为多字节字符集,读取的仍然是乱码
      

  8.   

    fseek(pFile,0,SEEK_SET); 
    fread(pBuf,1,len,pFile);
    pBuf[len]=0;
      

  9.   

    7楼正解,读文件时是按字节读的,注意转换
    字符串长度要用_tcslen