读书人

关于vc储存串口传来的数据有关问题

发布时间: 2012-01-24 23:11:54 作者: rapoo

关于vc储存串口传来的数据问题
现在已经完成串口通讯 数据处理后存在变量中 请问如何将实时传来的数据保存在一个txt文件中阿 如果有具体的程序更好 急用拜谢

[解决办法]
CString strFileName = " ";
strFileName.Format( "输出文件.txt ",strTextName);
ofstream outFile(strFileName,ios_base::out | ios_base::app);
if (!outFile.good())
{
cerr < < "Error while opening output file " < < endl;
return -1;
}

CString str = " ";
把读到的内容格式为想要的内容存放在str中
outFile < < str < < endl;
[解决办法]
用CFile比较简单

CFile destFile;
CFileException ex;
CStringpath; //txt文件保存路径
char buf[1024];//缓冲区变量,大小根据实际情况

...

if (!destFile.Open(path,
CFile::modeWrite|
CFile::shareExclusive|
CFile::typeBinary|
CFile::modeCreate, &ex))
{
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
return;
}

destFile.Write(buf, sizeof(buf));

destFile.Close();

读书人网 >VC/MFC

热点推荐