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;
}
{
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;
}
{
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();
}