文件流的练习
RReadStream 与 RWriteStream 是用于操作流的基类,对于文件的操作流来说有 RFileReadStream 和 RFileWriteStream
对于流来说可以通过 >> 与 << 符号分别用于读出及写入,如果用 << 或 >> 一定要记得类型的匹配,例如:
TInt aId=10;
aStream<<aId; 这样就会报错,报
\Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(290) : error C2228: left of '.ExternalizeL' must have class/struct/union type \Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(333) : see reference to function template instantiation 'void __cdecl DoExternalizeL(const int &,class RWriteStream &,class Externalize::Member)' being compiled \Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(293) : error C2228: left of '.InternalizeL' must have class/struct/union type \Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(336) : see reference to function template instantiation 'void __cdecl DoInternalizeL(int &,class RReadStream &,class Internalize::Member)' being compiled \Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(290) : error C2664: 'ExternalizeL' : cannot convert parameter 1 from 'class RWriteStream' to 'class RFileWriteStream &' A reference that is not to 'const' cannot be bound to a non-lvalue \Symbian\8.0a\S60_2nd_FP2_SC\EPOC32\INCLUDE\s32strm.inl(333) : see reference to function template instantiation 'void __cdecl DoExternalizeL(const struct TUserGroup &,class RWriteStream &,class Externalize::Member)' being compiled 这种系统错误,导致无法查错,如果改成 TInt32 就不会报错,或把 << 换成 writeInt16 ,这样也不会出错,编译器就知道该怎么操作了 |
所以对于这种操作最好是通过 WriteXXX 或 ReadXXX 来操作(XXX 代表一个总的,可以换成 Int32 或 Int16 或 Real16)
- 文件流要通过 Open 或 Replace 去创建,创建完后要通过 PushL 方法压入栈中,这个 PushL 是流对像提供的,
用完后要 CommitL 提交更新,然后执行 Pop() 和 Release() 去释放空间 - 如果要把一个类对像流入或流出,这个类一定要实现 ExternalizeL 及 InternalizeL 方法,这两个方法的定义
void ExternalizeL(RWriteStream& aStream) const;void InternalizeL(RReadStream& aStream);
以下是练习代码
class TUserGroup rStream.Pop(); |
安平2009@原创
qi_jianzhou@126.com