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()