从文件中读取日志信息,将其封装成ACE_Message_Block组成的复合消息,然后将消息在界面上显示出来。
#pragma once
#pragma comment(lib,"ACEd.lib")
#include "ACE/OS.h"
#include "ACE/ACE.h"
#include "ACE/Message_Block.h"
#include <fstream>
#define BUFSIZE 400 + 14//注意Message_Block分配的空间是要比实际的数据大,否则在release的时候将会出错!
//400的话就会有错!
typedef struct
{
int nDataSize;
char chRealData[400];
} MYDATAFRAME;//数据缓冲
int main(int argc,char * argv[])
{
//////////////////////////////////////////////////////////////////////////
/// 读取Log文件,将Log文件的内容封装成复合消息(ACE_Message_Block)
/// 并显示出来
//////////////////////////////////////////////////////////////////////////
ACE_Message_Block * head = new ACE_Message_Block(BUFSIZE);
ACE_Message_Block * mblk = head;
std::ifstream ifLogFile;
ifLogFile.open("ace.log",ios::in);
MYDATAFRAME m_frameDataBuffer;
ACE_OS::memset(m_frameDataBuffer.chRealData,'\0',400);
m_frameDataBuffer.nDataSize = 0;
int nReadCnt = 0;
while(!ifLogFile.eof())
{
ifLogFile.read(m_frameDataBuffer.chRealData,REAL_DATA_SIZE);
nReadCnt = ifLogFile.gcount();
//gcount函数:返回上次"非格式话读取操作"所读入的字符的个数
m_frameDataBuffer.nDataSize=nReadCnt;
int count = ::sprintf(mblk->wr_ptr(),m_frameDataBuffer.chRealData);
mblk->wr_ptr(nReadCnt);//注意这里不能用::sprintf返回的count,这个数值是不准确的!,而是使用gcount函数返回的nReadCnt
ACE_OS::memset(m_frameDataBuffer.chRealData,'\0',400);
m_frameDataBuffer.nDataSize = 0;
if (nReadCnt <= 0)
{
break;
}
mblk->cont(new ACE_Message_Block(BUFSIZE));//组成消息链,复合消息
mblk = mblk->cont();
}
ifLogFile.close();
for (mblk = head; mblk != 0;mblk = mblk->cont())
{
ACE::write_n(ACE_STDOUT,mblk->rd_ptr(),mblk->length());
}
head->release();//释放空间,注意这里是全部消息链上的空间都进行了释放!
getchar();
return 0;
}
|
阅读(1078) | 评论(0) | 转发(0) |