求助:MFC编程多线程 数据写入文本出错 提示“Buffer too small”
我大概10ms左右向文本写入一次数据,每次都可以运行,但是运行几分钟后,就提示出错“Buffer too small”,检查过写入到文本中的数据,是正确的,就是写入时间一长不到10分钟左右就提示错误。另外在我的工程中,涉及到多线程,文本记录专门有一个文本记录线程负责,下面是我代码里面被文本记录线程调用的,记录函数的内容。请大家帮我看看,我的问题出在哪儿,谢谢大家。如果把文本记录函数中,写入文本的代码全部注释掉,就剩下打开和关闭文本的话,运行很正常。请大家帮我看看,谢谢。void COPCServerMFC2Dlg::DataRecord(CString IP,CString DeviceName,CString SendData,CString ReceiveData)
{
CString s_time;
s_time= _T("F:\\OPC-Data\\");     //存放路径
CTime t=CTime::GetCurrentTime();
s_time+=t.Format("%Y-%m-%d");     //文本以日期命名
s_time+=_T(".txt");              //文本后缀名
LPCTSTR FileName = s_time;
CStdioFile myFile;
CFileException fileException;
if(myFile.Open(FileName,CFile::typeText|CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate),&fileException)
{
//1时间
myFile.SeekToEnd();
WaitForSingleObject( hMutex, INFINITE);
CTime t=CTime::GetCurrentTime();
stime=t.Format("%Y-%m-%d %H:%M:%S");
stime.Format(_T("%-25s\t"),stime);
myFile.WriteString (stime);
ReleaseMutex( hMutex);
//2IP
myFile.SeekToEnd();
IP.Format(_T("%-20s\t"),IP);
myFile.WriteString(IP);
//3DeviceName
myFile.SeekToEnd();
DeviceName.Format(_T("%-15s\t"),DeviceName);
myFile.WriteString(DeviceName);
//4 Send Data
myFile.SeekToEnd();
SendData+="Send!";
SendData.Format(_T("%-20s\t"), SendData);
myFile.WriteString( SendData);
//5Receive Data
ReceiveData+="Receive!";
myFile.SeekToEnd();
ReceiveData.Format(_T("%-20s\t\n"),ReceiveData);
myFile.WriteString(ReceiveData);
myFile.Close();
}
else
{
TRACE(_T("Can't open file %s,error=%u"),FileName,fileException.m_cause);
}  
}
  下面是存放到文本里面的数据:
2011-11-09 14:53:01  127.0.0.1  Device35  30 30 00 00 00 06 41 03 00 01 00 01 Send!  30 30 00 00 00 05 41 03 02 00 00 Receive!

解决方案 »

  1.   

    stime.Format(_T("%-25s\t"),stime);估计是这里的问题。
      

  2.   

    LuciferStar
    谢谢你的回答,能详细讲讲吗?我刚开始编MFC,请多指教,谢谢。
      

  3.   

    This function formats and stores a series of characters and values in the CStringT. Each optional argument (if any) is converted and output according to the corresponding format specification in pszFormat or from the string resource identified by nFormatID. The call will fail if the string object itself is offered as a parameter to Format. For example, the following code will cause unpredictable results: CAtlString str = _T("Some Data");
    str.Format(_T("%s%d"), str, 123);   
    // Attention: str is also used in the parameter list.