vc Symbian开发23.docx
《vc Symbian开发23.docx》由会员分享,可在线阅读,更多相关《vc Symbian开发23.docx(10页珍藏版)》请在冰豆网上搜索。
vcSymbian开发23
symbian学习笔记二十三:
文件读写例子--未审核
编辑文档
在给出一个关于读写的完整例子之前,我们首先要介绍一个编写程序时的小工具用法。
也许您与我一样,在编写Symbian下例子时突然会发现有一些类是我们以前没有介绍过或者说书上没有过多介绍的类,我们甚至不知道这些类属于哪个头文件以及属于哪个lib库,不得不说这让我非常苦恼,因为每一次遇到这种情况,我都要上网出查,很费时间,难道VC++6这个无比出名的开发工具就没有提供任何查询工具吗?
当然,答案是否定的,在笔记
(1)中我们就曾经说过配置symbian环境时,我们会提取一个小的工具条放置在VC6中——
而这个工具条中的那个放大镜(FindSymbol)就是干这个用的,呵呵,具体使用请看下面的例子:
根据上图,我们可知,只要选中我们要查找的类(CFileStore)然后点击工具条中的“放大镜(FindSymbol)”,就会出现中间的对话框,其中给出了该类所在的头文件名称以及该类实现所在的lib库文件名称。
哈哈~,怎么样,一下子方便了不少吧,当然,其实方便也是有限的,因为当你发现你少加载了相应的lib库文件,你不得不去修改项目文件(.mmp)从而加载lib库文件并重新建立项目(当然,重新建立项目也不会把你写好的代码文件删除,这点可以放心啦,呵呵)
//============FileStore.cpp================
#include"CommonFramework.h"
#include
#include
#include
LOCAL_DconstKStuDataStreamId=0x101f613f;
LOCAL_DconstKTeachDataStreamId=0x101f6140;
//CStudentclassdefind
classCStudent:
publicCBase
{
public:
//在程序中构造类对象
staticCStudent*NewLC(TDesC&aName,TInt32aNo,TInt32aScore);
~CStudent();
//从文件中构建类对象
staticCStudent*NewLC(RReadStream&aStream);
public:
voidExternalizeL(RWriteStream&aStream)const;//外化
voidInternalizeL(RReadStream&aStream);//外化
voidDisplay();
CStudent();//无参构造函数
private:
CStudent(TInt32aNo,TInt32aScore);
voidConstructL(TDesC&aName);
private:
HBufC*iName;
TInt32iNo;//明确指明Int类型的位数
TInt32iScore;//明确指明Int类型的位数
};
CStudent*CStudent:
:
NewLC(TDesC&aName,TInt32aNo,TInt32aScore)
{
CStudent*self=new(ELeave)CStudent(aNo,aScore);//构造CStudent指针对象
CleanupStack:
:
PushL(self);//压入指针栈
self->ConstructL(aName);//赋值描述符,可能引起异常
CleanupStack:
:
Pop(self);//弹出指针栈
returnself;
}
CStudent:
:
CStudent(){}
CStudent:
:
CStudent(TInt32aNo,TInt32aScore)
{
iNo=aNo;//学号
iScore=aScore;//分数
}
voidCStudent:
:
ConstructL(TDesC&aName)
{
iName=aName.AllocL();//注意这里的赋值方法
}
CStudent:
:
~CStudent()//析构函数
{
deleteiName;
iName=NULL;
}
voidCStudent:
:
ExternalizeL(RWriteStream&aStream)const
{
aStream.WriteInt32L(iName->Des().MaxLength());//写入iName的最大长度
aStream<<*iName;//写入姓名
aStream.WriteInt32L(iNo);//写入学号
aStream.WriteInt32L(iScore);//写入分数
}
voidCStudent:
:
InternalizeL(RReadStream&aStream)
{
if(iName!
=NULL)//如果iName不为空
{
deleteiName;//释放iName资源(内存)
iName=NULL;
}
TInt32max;
max=aStream.ReadInt32L();//读取iName的最大长度
iName=HBufC:
:
NewL(aStream,max);//读取iName的名字
iNo=aStream.ReadInt32L();//读取学号
iScore=aStream.ReadInt32L();//读取分数
}
CStudent*CStudent:
:
NewLC(RReadStream&aStream)
{
CStudent*self=new(ELeave)CStudent;
CleanupStack:
:
PushL(self);
self->InternalizeL(aStream);//进行内化
CleanupStack:
:
Pop(self);
returnself;
}
voidCStudent:
:
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->Printf(_L("pressanykeytocontiune\n"));
console->Getch();
}
classCTeacher:
publicCBase
{
public:
//构造CTeacher指针对象(名字,学号,年龄)
staticCTeacher*NewLC(TDesC&aName,TInt32aNo,TInt32aAge);
~CTeacher();
//构造函数,用于读取文件
staticCTeacher*NewLC(RReadStream&aStream);
public:
voidExternalizeL(RWriteStream&aStream)const;
voidInternalizeL(RReadStream&aStream);
voidDisplay();
CTeacher();
private:
CTeacher(TInt32aNo,TInt32aAge);
voidConstructL(TDesC&aName);
private:
HBufC*iName;
TInt32iNo;
TInt32iAge;
};
CTeacher*CTeacher:
:
NewLC(TDesC&aName,TInt32aNo,TInt32aAge)
{
CTeacher*self=new(ELeave)CTeacher(aNo,aAge);
CleanupStack:
:
PushL(self);
self->ConstructL(aName);
CleanupStack:
:
Pop(self);
returnself;
}
CTeacher:
:
CTeacher(TInt32aNo,TInt32aAge)
{
iNo=aNo;
iAge=aAge;
}
CTeacher:
:
CTeacher(){};
voidCTeacher:
:
ConstructL(TDesC&aName)
{
iName=aName.AllocL();
}
CTeacher:
:
~CTeacher()
{
deleteiName;
iName=NULL;
}
voidCTeacher:
:
ExternalizeL(RWriteStream&aStream)const
{
aStream.WriteInt32L(iName->Des().MaxLength());//写入名称长度
aStream<<*iName;//写入姓名
aStream.WriteInt32L(iNo);//写入编号
aStream.WriteInt32L(iAge);//写入年龄
}
voidCTeacher:
:
InternalizeL(RReadStream&aStream)
{
if(iName!
=NULL)
{
deleteiName;
iName=NULL;
}
TInt32max;
max=aStream.ReadInt32L();//读取名字长度
iName=HBufC:
:
NewL(aStream,max);//根据长度,读取名称
iNo=aStream.ReadInt32L();//读取编号
iAge=aStream.ReadInt32L();//读取年龄
}
CTeacher*CTeacher:
:
NewLC(RReadStream&aStream)
{
CTeacher*self=new(ELeave)CTeacher;
CleanupStack:
:
PushL(self);
self->InternalizeL(aStream);
CleanupStack:
:
Pop(self);
returnself;
}
voidCTeacher:
:
Display()
{
_LIT(KNo,"No=%d\n");
_LIT(KAge,"Age=%d\n");
_LIT(KNewLine,"\n");
console->Printf(*iName);
console->Printf(KNewLine);
console->Printf(KNo,iNo);
console->Printf(KAge,iAge);
console->Printf(KNewLine);
console->Printf(_L("pressanykeytocontinue\n"));
console->Getch();
}
voidTSStoreL(RFsaFs,TDesC&aFileName)
{
_LIT(KName1,"gyb");
_LIT(KName2,"lwm");
_LIT(KName3,"ds");
_LIT(KName4,"wxf");
TBuf<10>Name1(KName1);
TBuf<10>Name2(KName2);
TBuf<10>Name3(KName3);
TBuf<10>Name4(KName4);
CStudent*stu1=CStudent:
:
NewLC(Name1,1,60);
CStudent*stu2=CStudent:
:
NewLC(Name2,2,80);
CTeacher*teacher1=CTeacher:
:
NewLC(Name3,1,23);
CTeacher*teacher2=CTeacher:
:
NewLC(Name4,2,24);
CArrayFixFlat*array1=new(ELeave)CArrayFixFlat(4);
CArrayFixFlat*array2=new(ELeave)CArrayFixFlat(4);
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();//建立流字典写入学生写入学生数据
RStoreWriteStreamStuStream;//写入流
TStreamIdSid=StuStream.CreateLC(*store);//创建store下的写入流
TInt32numberOfStudents=array1->Count();
StuStream<for(TInti=0;i{
(*array1)[i].ExternalizeL(StuStream);
}
StuStream.CommitL();
CleanupStack:
:
PopAndDestroy();
//写入教师数据
RStoreWriteStreamTeacherStream;//写入流
TStreamIdTid=TeacherStream.CreateLC(*store);
TInt32numberOfTeachers=array2->Count();
TeacherStream<for(i=0;i{
(*array2)[i].ExternalizeL(TeacherStream);
}
TeacherStream.CommitL();
CleanupStack:
:
PopAndDestroy();
//在流字典中建立流与Uid的映射
dictionary->AssignL(TUid:
:
Uid(KStuDataStreamId),Sid);
dictionary->AssignL(TUid:
:
Uid(KTeachDataStreamId),Tid);
RStoreWriteStreamroot;//根流
TStreamIdrootid=root.CreateLC(*store);//创建根流
root<<*dictionary;
root.CommitL();
CleanupStack:
:
PopAndDestroy();
CleanupStack:
:
PopAndDestroy();
store->SetRootL(rootid);//设置根流
store->CommitL();
CleanupStack:
:
PopAndDestroy();
}
voidTSRestoreL(RFsaFs,TDesC&aFileName)
{
CArrayFixFlat*array1=new(ELeave)CArrayFixFlat(10);
CleanupStack:
:
PushL(array1);
CArrayFixFlat*array2=new(ELeave)CArrayFixFlat(10);
CleanupStack:
:
PushL(array2);
//打开文件存储
CFileStore*store=CDirectFileStore:
:
OpenLC(aFs,aFileName,EFileRead);
CStreamDictionary*dictionary=CStreamDictionary:
:
NewLC();//创建流字典
RStoreReadStreamrootStream;//根流
rootStream.OpenLC(*store,store->Root());//打开根流
rootStream>>*dictionary;//读出流字典
CleanupStack:
:
PopAndDestroy();
TStreamIdSid=dictionary->At(TUid:
:
Uid(KStuDataStreamId));
TStreamIdTid=dictionary->At(TUid:
:
Uid(KTeachDataStreamId));
CleanupStack:
:
PopAndDestroy();
//读出学生数据
RStoreReadStreamreadStuStream;
readStuStream.OpenLC(*store,Sid);
TInt32numberOfStudents;
readStuStream>>numberOfStudents;
for(TInti=0;i{
CStudent*stu=CStudent:
:
NewLC(readStuStream);
CleanupStack:
:
PushL(stu);
array1->AppendL(*stu);
CleanupStack:
:
Pop(stu);
}
CleanupStack:
:
PopAndDestroy();
_LIT(KPrompt1,"Liststudents:
\n");
console->Printf(KPrompt1);
for(i=0;iCount();i++)
{
(*array1)[i].Display();
}
//读出教师数据
RStoreReadStreamreadTeacherStream;
readTeacherStream.OpenLC(*store,Tid);
TInt32numberOfTeachers;
readTeacherStream>>numberOfTeachers;
for(i=0;i{
CTeacher*teacher=CTeacher:
:
NewLC(readTeacherStream);
CleanupStack:
:
PushL(teacher);
array2->AppendL(*teacher);
CleanupStack:
:
Pop(teacher);
}
CleanupStack:
:
PopAndDestroy();
_LIT(KPrompt2,"Listteachers:
\n");
console->Printf(KPrompt2);
for(i=0;iCount();i++)
{
(*array2)[i].Display();
}
CleanupStack:
:
PopAndDestroy(store);
CleanupStack:
:
PopAndDestroy
(2);
}
voiddoExampleL()
{
RFsiFs;
User:
:
LeaveIfError(iFs.Connect());
_LIT(KFileName,"c:
\\stuList1.txt");
TBuf<20>FileName(KFileName);
TSStoreL(iFs,FileName);
TSRestoreL(iFs,FileName);
iFs.Close();
}
运行效果
在之后的学习中,我们将继续介绍有关文件读写方面的知识,大家可以先运行这个例子体会一下