int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
char   *buf ;//还有这句。
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
          //问题代码从这里开始。
CFile   f; 
 
if(FALSE==f.Open(_T(".\\EventLog.txt"),CFile::modeReadWrite))
{
AfxMessageBox(_T("Log file open failed!"));
return 0 ;
} unsigned int    size   = (unsigned int)  f.GetLength();//可能丢失数据
cout<< "size1:"<< f.GetLength()<<endl;
cout<<"size2:"<<size<<endl; buf  =   new   char[size+1  ];
if(NULL==buf)
{
AfxMessageBox(_T("Allocate memory failed!"));
return 0;
}
ZeroMemory(buf,size + 1);
f.Read(buf,size);//读取到的文件内容被严重截断.没有size 大是为什么? buf[sizeof(char)*size]='\0'; }
delete []buf;
while(true);
return nRetCode;
}

解决方案 »

  1.   

    可能是EventLog.txt文件编码的问题,buf中的内容乱码吗?
      

  2.   

    try
    {
     CFile file;
     file.Open(_T(".\\EventLog.txt"), CFile::modeRead);
     int len = (int)file.GetLength();
     char* buf = new char[len+1];
     memset(buf, 0, sizeof(char)*(len+1);
     file.Read(buf, len);
     file.Close();
     CString strText(buf);
     AfxMessageBox(strText);
     delete[] buf;
    }
    catch(CFileException* e)
    {
     e->ReportError();
     e->Delete();
    }
      

  3.   

    用记事本打开无乱码,用UltraEdit 打开有 乱码。