文件类和流操作

Location: s32file.h

文件模拟路径在C:/Symbian/8.0a/epoc32/wins下面。有C、D两个分区。

RFs fs;
     
     
User::LeaveIfError(fs.Connect());
     
     
RFile file
     
     
User::LeaveIfError(file.Open(fs, _L("C://file.foo"), EFileWrite));
     
     
TBuf8<256> buf;
     
     
file.Read(buf, 256);
     
     
file.Seek(ESeekStart, 911);
     
     
file.Write(_L8("Some thing you wanna write..."));
     
     
file.Close();
     
     

1)      与文件服务器建立通信

RFs fsSession;

TInt fsret = fsSession.Connect(); // start a file session

       if (fsret != KErrNone)

              {console->Printf(KTxtConnectFailed,fsret);

              User::Leave(fsret);

              }

  2确定文件路径存在

fsSession.MkDirAll(KFullNameOfFileStore); // make sure directory exists

     

 

  3建立文件存储

TParse       filestorename;// The class uses the full filename structure supported by Symbian

       fsSession.Parse(aName,filestorename);

/*------------------------------------------------------------------------------------------------

 TDesC& aName。可以通过以下方式给aNAME赋值:
    _LIT(aName,"C://epoc32ex//data//SimpleClassToSimpleStream.dat");

----------------------------------------------------------------------------------------------*/

                            // construct file store object - the file to contain the

                            // the store replaces any existing file of the same name.

       CFileStore*     //如果EFileRead为读出流

 store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);

store->SetTypeL(KDirectFileStoreLayoutUid); // 设定存储种类

 

4将外部数据写入流::(记忆方式:>>指向就是数据流向)//假设:TSimple anXxx;

RStoreWriteStream outstream;

       TStreamId id = outstream.CreateLC(*store);

//----------------------------将标量写入数据流------------------

outstream<< anXxx;

    或者 aStream.WriteInt8L(anXxx);

    实际上这里使用了流的扩展化:(当输出不是普通的元数据时,使用这个扩展化)这是一个虚函数的重载

void TSimple::ExternalizeL(RWriteStream& aStream) const

       {

       aStream << iTheEnum;

       aStream << iBuffer;

       aStream.WriteInt32L(iIntValue);

       aStream.WriteUint32L(iUintValue);

       aStream.WriteReal64L(iRealValue);

       } 

//------------------------------------------------------------------------------------------------

     // 以下是将流改动提交到文件服务器。

       outstream.CommitL();

5将流读到外部数据:

       RStoreReadStream instream;

store->SetRootL(id);//可以将上面的已经存在的流作为流的根。好处是不必再创建流ID。实际上也就节省了内存。

// Commit changes to the store

       store->CommitL();

// Construct and open the input stream object. We want to access the root stream from the store in this example.

       instream.OpenLC(*store,store->Root());

TSimple thesimple;

       instream >> thesimple;//写入类对象数据。

    //---------------------------------------------------------------------

void TSimple::InternalizeL(RReadStream& aStream)

       {

       aStream >> iTheEnum;

       aStream >> iBuffer;

         iIntValue  = aStream.ReadInt32L();

       iUintValue = aStream.ReadUint32L();

       iRealValue = aStream.ReadReal64L();

       } 

    //------------------------------输出流到其他数据元或类对象中----------------------------

     anXxx = TXxx(aStream.ReadInt8L());

  

6关闭文件服务通信

fsSession.Close()

   

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值