我使用了一个定时器,在回调函数中打开一个文件,并向文件中写入内容
代码如下:
void TdmPdmToolsReg::AutoEvaluate()
{
SetTimer(NULL,1,10000,(TIMERPROC)TimeProc);
}
void CALLBACK TdmPdmToolsReg::TimeProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime )         
{
    CString InfoFile=CPDMDatabase::GetInstallPath() + _T("EDMS\\SPT");
SetCurrentDirectory(InfoFile);//设置PDM系统配置文件目录为当前目录
CString InfoFilePath;
InfoFilePath.Format(_T("%s\\text.txt"), InfoFile);
if( !m_file.Open( InfoFilePath, CFile::modeCreate | CFile::modeWrite, &e ) )
{
AfxMessageBox("File could not be opened");
}
LPTSTR p;
         CString info="数据库..";
p=info.GetBuffer(info.GetLength());
m_file.SeekToEnd();
m_file.Write(p,info.GetLength() );
info.ReleaseBuffer();

}
但是,无论调用几次回调函数,但是文件里只显示一次"数据库..",而不是几次结果接连显示出来,不知道为什么,请高手解答一下。谢谢

解决方案 »

  1.   

    if( !m_file.Open( InfoFilePath, CFile::modeCreate | CFile::modeWrite, &e ) )
    {
    AfxMessageBox("File could not be opened");
    }
    以modeCreate方式打开文件时,是会将文件内容先清空后再写入的。所以你打开文件的方式不对。应该再增加CFile::modeNoTruncate,表明不用截断文件即可。
    if( !m_file.Open( InfoFilePath, CFile::modeCreate|CFile:modeNoTruncate | CFile::modeWrite, &e ) )
    {
    AfxMessageBox("File could not be opened");
    }
      

  2.   

    问题已经解决。
    谢谢happyparrot   :)