char appPath[256];
char *chatLog;
// memset(&chatLog,0,MAX_LEN);
m_strArray.RemoveAll(); GetCurrentDirectory(256,appPath);
CString filePath;
filePath.Format("%s",appPath);
filePath+="\\";
filePath+=CHATLOG;
    CFile file;    file.Open(filePath,CFile::modeRead); DWORD fileLen;
fileLen=file.GetLength();//这里出错
if(fileLen==0)
{
return;
}
chatLog=new char[fileLen+1];
chatLog[fileLen]=0;
file.Read(chatLog,fileLen);
file.Close();异常信息:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!Program: ...iles\MSDev98\MyProjects\NetWordClient\Debug\NetWordClient.exe
File: filecore.cpp
Line: 258For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.(Press Retry to debug the application)
---------------------------
终止(A)   重试(R)   忽略(I)   
---------------------------
如果我点击忽略,出现这样提示:---------------------------
NetWordClient
---------------------------
没有找到 一未命名文件。
---------------------------
确定   
---------------------------
但是怪我单步调试没这样的错误啊!!!就是直接执行exe文件有这样的错误!
谢谢!

解决方案 »

  1.   

    你先判断文件打开是否成功再GetLength呀
      

  2.   


    file.Open(filePath,CFile::modeRead);你怎么不在这里判断下值再往下走呢?你确定打开操作成功了fileLen=file.GetLength();//这里出错可能就是因为file没能成功打开
      

  3.   

    谢谢回复!!
    可能是我没说清楚,我是说单步调试没那样问题?直接执行exe就出现了!
      

  4.   

    filePath+=CHATLOG;是不是应该改为filePath+="CHATLOG";
    1)CHATLOG是不是合法的文件名;
    2)Open函数返回是否成功还没有判断,就调用GetLength肯定是不妥的;
    建议:if(!file.Open(filePath,CFile::modeRead)) return;
      

  5.   

    调试的程序路径和运行时的程序路径是不一样的,一个是根目录,一个是根目录\debug
    如果CHATLOG带有子目录名称,当然会出现找不到文件,
    你的程序学得很难看,而且不严谨,不仅没有异常处理,而且也有内存泄漏
      

  6.   

    我给你写一段比较标准的代码吧,标准c的比mfc的干净。
    string readlog()
    {
     try
    {
     char workpath[MAX_PATH];
     GetModuleFileName(0,path,sizeof workpath);
     char filename[MAX_PATH];
     sprintf_s(filename,sizeof filename,"%s\\%s",workpath,CHATLOG);
     ifstream ifs;
     ifs.open(filename,ios:load);
     if(!ifs.is_open())
       throw string("file not find");
     ifs.seekg(0,ios:end);
     int filelen = ifs.tellg();
     auto_ptr<char> buf(new char[filelen]);
     ifs.seekg(0,ios:beg);
     ifs.read(buf.get(),filelen);
     string rs;
     rs.append(buf.get(),filelen);
     return rs;
     }
     catch(string er)
    {
     trace("readlog failed:%s\n",er.c_str());
    }
    }
      

  7.   

    比如说CFile f;
    CFileException e;
    TCHAR* pFileName = _T("test.dat");
    if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
      {
    #ifdef _DEBUG
      ::MessageBox(NULL,"dsjfls","error",1);
    #endif
      }
      

  8.   

    //Cfile and   CArchive (关联)
    //widows ApI
    // VC File function
    //CFile