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文件有这样的错误!
谢谢!
file.Open(filePath,CFile::modeRead);你怎么不在这里判断下值再往下走呢?你确定打开操作成功了fileLen=file.GetLength();//这里出错可能就是因为file没能成功打开
可能是我没说清楚,我是说单步调试没那样问题?直接执行exe就出现了!
1)CHATLOG是不是合法的文件名;
2)Open函数返回是否成功还没有判断,就调用GetLength肯定是不妥的;
建议:if(!file.Open(filePath,CFile::modeRead)) return;
如果CHATLOG带有子目录名称,当然会出现找不到文件,
你的程序学得很难看,而且不严谨,不仅没有异常处理,而且也有内存泄漏
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());
}
}
CFileException e;
TCHAR* pFileName = _T("test.dat");
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
{
#ifdef _DEBUG
::MessageBox(NULL,"dsjfls","error",1);
#endif
}
//widows ApI
// VC File function
//CFile