流
如果我们希望把数据传输到文本文件或其他外部设备,则需要”外部化”,反之则为”内部化”,有两个基类来实现这种外部化和内部化工作,RReadStream和RWriteStream。对应每一种介质:文件、描述符、内存缓存。则有不同的方法对(方法总是成对出现的):
1. 文件
RFileReadStream
RFileWriteStream
2. 描述符
RDesReadStream
RDesWriteStream
3. 缓存
RBufReadStream
RBufWriteStream
4. 内存
RMemReadStream
RMemWriteStream
另外,如果希望把创建的推入清除栈以保证其异常退出安全,则需要调用流的PushL().
外部化对应的符号是:<<,需要重写ExternalizeL()
内部化对应的符号是:>>,需要重写InternalizeL()
一个完整的流操作的例子:
#include
class CStreamSample:public CBase
{
public:
void ExternalizeL(RReadStream& arStream)
const;
void InternalizeL(RWriteStream& awStream)
cosnt;
public:
Tint iIntValue;
TBuf<64>iBuffer;
};
void CStreamSample::ExternalizeL(RReadStream& aStram)
const
{
aStream.WriteInt32L(iIntVal); //这个函数是什么意思?
aStream<
}
void CStreamSample::InternalizeL(RWriteStream& aStream)
const
{
iIntVal=aStreamm.WriteInt32L();
aStream>>iBuffer;
}
…
void SampleFuncL(TAny* a,Tint aMaxLen)
{
//为一个描述符分配一个空间
TBuf buf;
buf.Create(64);
buf.CleanupClosePushL();
buf.Append(KSampleBuffer,1);
//
CStreamSample* sSample=newLC(1,buf);//这又是?
RMemWriteStream write;
write.PushL();
write.Open(a,aMaxLen);
write<<*sSample;
write.CommitL();
CleanupStack::PopAndDestory(&write);
buf=KSampelBuffer2;
CStreamSample* sSample2=newLC(2,buf);
RMemReadStream read;
read.PushL();
read.Open(a,aMaxLen);
read>>*sSample2;
read.CommitL();
CleanupStack::PopAndDestory(4); //….4??why is 4
}
阅读(1005) | 评论(1) | 转发(0) |