文件流的练习

文件流的练习

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)

  1. 文件流要通过 Open 或 Replace 去创建,创建完后要通过 PushL 方法压入栈中,这个 PushL 是流对像提供的,
    用完后要 CommitL 提交更新,然后执行 Pop() 和 Release() 去释放空间
  2. 如果要把一个类对像流入或流出,这个类一定要实现 ExternalizeL 及 InternalizeL 方法,这两个方法的定义
    void ExternalizeL(RWriteStream& aStream) const;
    void InternalizeL(RReadStream& aStream);

以下是练习代码

class TUserGroup
{
public:
    TInt16 iGroupId;
    TBuf16
<20> iGroupName;
    
void ExternalizeL(RWriteStream& aStream) const
    {                
        aStream.WriteInt16L(iGroupId);
        aStream
<<iGroupName;
    }

    
void InternalizeL(RReadStream& aStream)
    {
        iGroupId 
= aStream.ReadInt16L();
        aStream
>>iGroupName;
    }
};


void useStream()
{
    TFileName filename;
    _LIT(KFilename,
"c:\\a.txt");
    filename.Copy(KFilename);
    RFs rf;
    User::LeaveIfError(rf.Connect());

    RFileWriteStream stream;
    stream.Replace(rf,filename,EFileWrite);
    stream.PushL();

    TInt16 aId 
= 10;
    _LIT(KGroupName,
"groupName");
    _LIT(KGroup2,
"beijin");
    TBuf16
<20> gn(KGroupName);
    stream.WriteInt16L(aId);
    stream
<<gn;

    TUserGroup ug;
    ug.iGroupId 
= 50;
    gn.Zero();
    gn.Copy(KGroup2);
    ug.iGroupName 
= gn;

    stream
<<ug;
    
// 提交更改并释放资源
    stream.CommitL();
    stream.Pop();
    stream.Release();

    
// 将数据清空
    aId = 0;
    gn.Zero();

    
// 读取数据
    RFileReadStream rStream;
    rStream.Open(rf,filename,EFileRead);
    _LIT(KRead,
"read data:\n"); 
    console
->Printf(KRead);
    aId 
= rStream.ReadInt16L();
    rStream
>>gn;

    _LIT(Kfmt,
"id=%d\ngroupname=%S\n");
    console
->Printf(Kfmt,aId,&gn);

    rStream
>>ug;
    console
->Printf(Kfmt,ug.iGroupId,&ug.iGroupName);

    rStream.Pop();
    rStream.Release();

    rf.Close();
}

 



安平2009@原创
qi_jianzhou@126.com

原文地址:https://www.cnblogs.com/zziss/p/1652803.html