📄 filestore.cpp
字号:
// FileStore.cpp
//
// Copyright (c) 2005 Neusoft Institute of Information Technology.Chengdu
// All rights reserved.
// written by hewei
//
// version 1.0
//Date: 2005-11-14
// This program demostrate how to use file store and stream dictionary.
// NOTE: the structure of this example is different to standard E32 examples
#include <e32cons.h>
#include <f32file.h>
#include <e32std.h>
#include <s32file.h>
#include <s32stor.h>
#include <s32std.h>
// All messages written to this
LOCAL_D CConsoleBase* console;
LOCAL_D const KStuDataStreamId=0x101F613F;
LOCAL_D const KTeachDataStreamId=0x101F6140;
// Function prototypes
void callExampleL();
void doExampleL();
/////////////////////////////////////////////////////////////////
//
//CStudent class definition
//
/////////////////////////////////////////////////////////////////
class CStudent :public CBase
{
public:
static CStudent* NewL(TDesC& aName,TInt32 aNo,TInt32 aScore);
static CStudent* NewLC(TDesC& aName,TInt32 aNo,TInt32 aScore);
~CStudent();
static CStudent* NewL(RReadStream& aStream); //带有读取流参数的NewL和NewLC
static CStudent* NewLC(RReadStream& aStream); //函数
public:
void ExternalizeL(RWriteStream& aStream)const; //外化
void InternalizeL(RReadStream& aStream); //内化
void Display(); //显示
CStudent();
private:
CStudent(TInt32 aNo,TInt32 aScore);
void ConstructL(TDesC& aName);
private:
HBufC* iName;
TInt32 iNo; //是TInt32不是TInt
TInt32 iScore; //是TInt32不是TInt
};
/////////////////////////////////////////////////////////////////////
//
//Cstudent class implemention
//
//////////////////////////////////////////////////////////////////////
CStudent* CStudent::NewL(TDesC& aName,TInt32 aNo,TInt32 aScore)
{
CStudent* self=CStudent::NewLC(aName,aNo,aScore);
CleanupStack::Pop(self);
return self;
}
CStudent* CStudent::NewLC(TDesC& aName,TInt32 aNo,TInt32 aScore)
{
CStudent* self=new (ELeave) CStudent(aNo,aScore);
CleanupStack::PushL(self);
self->ConstructL(aName);
return self;
}
CStudent::CStudent(TInt32 aNo,TInt32 aScore)
{
iNo=aNo;
iScore=aScore;
}
CStudent::CStudent()
{
}
void CStudent::ConstructL(TDesC& aName)
{
iName=aName.AllocL();
}
CStudent::~CStudent()
{
delete iName;
iName=NULL;
}
void CStudent::ExternalizeL(RWriteStream& aStream)const
{
aStream.WriteInt32L(iName->Des().MaxLength()); //写入iName的最大长度
aStream<<*iName;//外化姓名
aStream.WriteInt32L(iNo); //写入学号
aStream.WriteInt32L(iScore); //写入成绩
}
void CStudent::InternalizeL(RReadStream& aStream)
{
if(iName!=NULL)
{
delete iName;
iName=NULL;
}
TInt32 max;
max=aStream.ReadInt32L(); //读入最大长度
iName=HBufC::NewL(aStream,max);//从流中建立iName对象
iNo=aStream.ReadInt32L();//读入学号
iScore=aStream.ReadInt32L();//读入成绩
}
CStudent* CStudent::NewL(RReadStream& aStream)
{
CStudent* self=CStudent::NewLC(aStream);
CleanupStack::Pop();
return self;
}
CStudent* CStudent::NewLC(RReadStream& aStream)
{
CStudent* self=new (ELeave) CStudent;
CleanupStack::PushL(self);
self->InternalizeL(aStream);//内化
return self;
}
void CStudent::Display()
{
_LIT(KNo,"No=%d\n");
_LIT(KScore,"score=%d\n");
_LIT(KNewLine,"\n");
console->Printf(*iName);
console->Printf(KNewLine);
console->Printf(KNo,iNo);
console->Printf(KScore,iScore);
console->Printf(KNewLine);
console->Getch();
}
/////////////////////////////////////////////////////////////////
//
//CTeacher class definition
//
/////////////////////////////////////////////////////////////////
class CTeacher :public CBase
{
public:
static CTeacher* NewL(TDesC& aName,TInt32 aNo,TInt32 aAge);
static CTeacher* NewLC(TDesC& aName,TInt32 aNo,TInt32 aAge);
~CTeacher();
static CTeacher* NewL(RReadStream& aStream); //带有读取流参数的NewL和NewLC
static CTeacher* NewLC(RReadStream& aStream); //函数
public:
void ExternalizeL(RWriteStream& aStream)const; //外化
void InternalizeL(RReadStream& aStream); //内化
void Display(); //显示
CTeacher();
private:
CTeacher(TInt32 aNo,TInt32 aAge);
void ConstructL(TDesC& aName);
private:
HBufC* iName;
TInt32 iNo; //是TInt32不是TInt
TInt32 iAge; //是TInt32不是TInt
};
/////////////////////////////////////////////////////////////////////
//
//CTeacher class implemention
//
//////////////////////////////////////////////////////////////////////
CTeacher* CTeacher::NewL(TDesC& aName,TInt32 aNo,TInt32 aAge)
{
CTeacher* self=CTeacher::NewLC(aName,aNo,aAge);
CleanupStack::Pop(self);
return self;
}
CTeacher* CTeacher::NewLC(TDesC& aName,TInt32 aNo,TInt32 aAge)
{
CTeacher* self=new (ELeave) CTeacher(aNo,aAge);
CleanupStack::PushL(self);
self->ConstructL(aName);
return self;
}
CTeacher::CTeacher(TInt32 aNo,TInt32 aAge)
{
iNo=aNo;
iAge=aAge;
}
CTeacher::CTeacher()
{
}
void CTeacher::ConstructL(TDesC& aName)
{
iName=aName.AllocL();
}
CTeacher::~CTeacher()
{
delete iName;
iName=NULL;
}
void CTeacher::ExternalizeL(RWriteStream& aStream)const
{
aStream.WriteInt32L(iName->Des().MaxLength()); //写入iName的最大长度
aStream<<*iName;//外化姓名
aStream.WriteInt32L(iNo); //写入学号
aStream.WriteInt32L(iAge); //写入年龄
}
void CTeacher::InternalizeL(RReadStream& aStream)
{
if(iName!=NULL)
{
delete iName;
iName=NULL;
}
TInt32 max;
max=aStream.ReadInt32L(); //读入最大长度
iName=HBufC::NewL(aStream,max);//从流中建立iName对象
iNo=aStream.ReadInt32L();//读入学号
iAge=aStream.ReadInt32L();//读入成绩
}
CTeacher* CTeacher::NewL(RReadStream& aStream)
{
CTeacher* self=CTeacher::NewLC(aStream);
CleanupStack::Pop();
return self;
}
CTeacher* CTeacher::NewLC(RReadStream& aStream)
{
CTeacher* self=new (ELeave) CTeacher;
CleanupStack::PushL(self);
self->InternalizeL(aStream);//内化
return self;
}
void CTeacher::Display()
{
_LIT(KNo,"No=%d\n");
_LIT(KScore,"Age=%d\n");
_LIT(KNewLine,"\n");
console->Printf(*iName);
console->Printf(KNewLine);
console->Printf(KNo,iNo);
console->Printf(KScore,iAge);
console->Printf(KNewLine);
console->Getch();
}
//////////////////////////////////////////////////////////////////////////////
//
// Main function called by E32
//
//////////////////////////////////////////////////////////////////////////////
GLDEF_C TInt E32Main()
{
// Get cleanup stack
CTrapCleanup* cleanup=CTrapCleanup::New();
// Some more initialization, then do the example
TRAPD(error,doExampleL());
// callExampleL() should never leave.
_LIT(KMsgPanicEpoc32ex,"EPOC32EX");
__ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
// destroy the cleanup stack
delete cleanup;
// return
return 0;
}
//////////////////////////////////////////////////////////////////////////////
//
//DoExample()
//
//////////////////////////////////////////////////////////////////////////////
void doExampleL()
{
_LIT(KPressAnyKey,"[Press any key...OK]");
//Get Console;
_LIT(KMsgExampleCode,"Symbian OS Example Code");
console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
// Put console onto the cleanup stack.
CleanupStack::PushL(console);
int error;
TRAP(error,callExampleL());
if(error)
{
_LIT(KERROR,"error occured!\n");
console->Printf(KERROR);
}
else{
_LIT(KNOLEAVE,"No Leave!\n");
console->Printf(KNOLEAVE);
}
console->Printf(KPressAnyKey);
console->Getch();
// Remove the console object from the cleanupstack
// and destroy it.
CleanupStack::PopAndDestroy();
}
void TSStoreL(RFs aFs,TDesC& aFileName)
{
_LIT(KNAME1,"hewei");
_LIT(KNAME2,"yangzongde");
_LIT(KNAME3,"zhangbing");
_LIT(KNAME4,"liyong");
TBuf<10> Name1(KNAME1);
TBuf<10> Name2(KNAME2);
TBuf<10> Name3(KNAME3);
TBuf<10> Name4(KNAME4);
CStudent* stu1=CStudent::NewL(Name1,1,60);
CStudent* stu2=CStudent::NewL(Name2,2,70);
CTeacher* teacher1=CTeacher::NewL(Name3,1,25);
CTeacher* teacher2=CTeacher::NewL(Name4,2,30);
CArrayFixFlat<CStudent> * array1=new (ELeave) CArrayFixFlat<CStudent>(4);
CArrayFixFlat<CTeacher> * array2=new (ELeave) CArrayFixFlat<CTeacher>(4);
//add three students to array using AppendL
array1->AppendL(*stu1);
array1->AppendL(*stu2);
array2->AppendL(*teacher1);
array2->AppendL(*teacher2);
CFileStore* store=CDirectFileStore::ReplaceLC(aFs,aFileName,EFileWrite);//建立直接文件存储
store->SetTypeL(TUidType(store->Layout()));//设置存储类型
CStreamDictionary* dictionary=CStreamDictionary::NewLC();//建立流字典
RStoreWriteStream StuStream; //写入流
TStreamId Sid=StuStream.CreateLC(*store);//创建store下的写入流
TInt32 numberOfStudents=array1->Count();
StuStream<<numberOfStudents;//写入数组长度
for(TInt i=0;i<numberOfStudents;i++)
{
(*array1)[i].ExternalizeL(StuStream);//外化数组元素
}
StuStream.CommitL();//提交
CleanupStack::PopAndDestroy();//流
RStoreWriteStream TeacherStream; //写入流
TStreamId Tid=TeacherStream.CreateLC(*store);//创建store下的写入流
TInt32 numberOfTeachers=array2->Count();
TeacherStream<<numberOfTeachers;//写入数组长度
for(i=0;i<numberOfTeachers;i++)
{
(*array2)[i].ExternalizeL(TeacherStream);//外化数组元素
}
TeacherStream.CommitL();//提交
CleanupStack::PopAndDestroy();//流
dictionary->AssignL(TUid::Uid(KStuDataStreamId),Sid);//在流字典中建立流与Uid的映射
dictionary->AssignL(TUid::Uid(KTeachDataStreamId),Tid);//在流字典中建立流与Uid的映射
RStoreWriteStream root;//根流
TStreamId rootid=root.CreateLC(*store);
root<<*dictionary;//外化流字典
root.CommitL();//提交
CleanupStack::PopAndDestroy();//root
CleanupStack::PopAndDestroy();//dictionary;
store->SetRootL(rootid);//设置根流
store->CommitL();//提交
CleanupStack::PopAndDestroy();//store
}
void TSRestoreL(RFs aFs,TDesC& aFileName)
{
CArrayFixFlat<CStudent> * array1=new (ELeave) CArrayFixFlat<CStudent>(10);//建立动态数组
CleanupStack::PushL(array1);
CArrayFixFlat<CTeacher> * array2=new (ELeave) CArrayFixFlat<CTeacher>(10);//建立动态数组
CleanupStack::PushL(array2);
CFileStore *store=CDirectFileStore::OpenLC(aFs,aFileName,EFileRead);//打开文件存储
CStreamDictionary* dictionary=CStreamDictionary::NewLC();//创建流字典
RStoreReadStream rootStream;
rootStream.OpenLC(*store,store->Root());//打开根流
rootStream>>*dictionary;//读出流字典
CleanupStack::PopAndDestroy();//root;
TStreamId Sid=dictionary->At(TUid::Uid(KStuDataStreamId));//获得学生流ID
TStreamId Tid=dictionary->At(TUid::Uid(KTeachDataStreamId));//获得教师流ID
CleanupStack::PopAndDestroy();//dic
//读出学生数据
RStoreReadStream readStuStream;
readStuStream.OpenLC(*store,Sid); //输入流
TInt32 numberOfStudents;
readStuStream>>numberOfStudents;//读入数组长度
for(TInt i=0;i<numberOfStudents;i++)
{
CStudent* stu=CStudent::NewL (readStuStream);//读出一个数据
CleanupStack::PushL(stu);
array1->AppendL(*stu); //添加到数组中
CleanupStack::Pop(stu);
}
CleanupStack::PopAndDestroy();//readStuStream
//打印学生信息
_LIT(KPrompt1,"List students:\n");
console->Printf(KPrompt1);
for(i=0;i<array1->Count();i++)
{
(*array1)[i].Display();
}
//读出教师数据
RStoreReadStream readTeacherStream;
readTeacherStream.OpenLC(*store,Tid); //输入流
TInt32 numberOfTeachers;
readTeacherStream>>numberOfTeachers;//读入数组长度
for(i=0;i<numberOfTeachers;i++)
{
CTeacher* teacher=CTeacher::NewL (readTeacherStream);//读出一个数据
CleanupStack::PushL(teacher);
array2->AppendL(*teacher); //添加到数组中
CleanupStack::Pop(teacher);
}
CleanupStack::PopAndDestroy();//readTeacherStream
//打印教师信息
_LIT(KPrompt2,"List teachers:\n");
console->Printf(KPrompt2);
for(i=0;i<array2->Count();i++)
{
(*array2)[i].Display();
}
CleanupStack::PopAndDestroy(store);//store
CleanupStack::PopAndDestroy(2);//array
}
void callExampleL()
{
RFs iFs;//文件会话
User::LeaveIfError(iFs.Connect());//connect to file server
_LIT(KFileName,"c:\\stuList1.txt");
TBuf<20> FileName(KFileName);
TSStoreL(iFs,FileName); //存储数据
TSRestoreL(iFs,FileName);//读出数据
//Close file server
iFs.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -