求助: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!
我大概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!
解决方案 »
- 透视投影矩阵和基础矩阵在图象匹配中的作用
- 已经知道bitmapinfo如何得到Cbitmap
- SetWindowExt和SetViewportExt 是不是两个函数都要同时调用,单独调用一个没有效果?
- DirectX 9.0 SDK (Summer 2004)\Include\strmif.h(1018) : error C2146: syntax error : missing ';' before identifier 'HSEMAPHORE
- 如何用模板<class>实现一个树类型
- 莫名其妙就多了5分性欲分,散一散。
- DirectX3D错误?
- 请问
- 如何正确使用资源
- 大大们帮帮忙啊
- 数字图像处理求助~~~
- 毕业了,给自己找份工作。。。
谢谢你的回答,能详细讲讲吗?我刚开始编MFC,请多指教,谢谢。
str.Format(_T("%s%d"), str, 123);
// Attention: str is also used in the parameter list.