⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filerw.cpp

📁 《基于symbian os的手机开发与应用》
💻 CPP
字号:
// FileRW.cpp
//
// Copyright (c) 2005 Neusoft Institute of Information Technology.Chengdu  
// All rights reserved.

// written by hewei
//  
// version 1.0

//Date: 2005-11-01

// This program demostrate how to read data from and write data to a file. 

// NOTE: the structure of this example is different to standard E32 examples


#include <e32cons.h>
#include <f32file.h>
#include <e32std.h>
#include <s32file.h>

  // All messages written to this
LOCAL_D CConsoleBase* console;

// Function prototypes
 void callExampleL();
 void doExampleL();
 

 /////////////////////////////////////////////////////////////////
 //
 //CStudent class definition
 //
 /////////////////////////////////////////////////////////////////
 class CStudent :public CBase
 {
  public:
	  static CStudent* NewL(TDesC& aName,TInt aNo,TReal aScore);
	  static CStudent* NewLC(TDesC& aName,TInt aNo,TReal aScore);
	  ~CStudent();
  
	  static CStudent* NewL(RReadStream& aStream);   //带有读取流参数的NewL和NewLC
      static CStudent* NewLC(RReadStream& aStream);  //函数
  public:	  
	 void ExternalizeL(RWriteStream& aStream); //外化
	 void InternalizeL(RReadStream&  aStream); //内化
	 void Display(); //显示
  private:
	  CStudent();
	  CStudent(TInt aNo,TReal aScore);
	  void ConstructL(TDesC& aName);
	 
  private:
	  HBufC* iName;
	  TInt iNo;
	  TReal iScore;
 };
 /////////////////////////////////////////////////////////////////////
 //
 //Cstudent class implemention
 //
 //////////////////////////////////////////////////////////////////////
 CStudent* CStudent::NewL(TDesC& aName,TInt aNo,TReal aScore)
 {
     
    CStudent* self=CStudent::NewLC(aName,aNo,aScore);
	CleanupStack::Pop();
	 return self;
 }
 CStudent* CStudent::NewLC(TDesC& aName,TInt aNo,TReal aScore)
 {
	 
	 CStudent* self=new (ELeave) CStudent(aNo,aScore);
	 CleanupStack::PushL(self);
     self->ConstructL(aName);
	 return self;
 }
 CStudent::CStudent(TInt aNo,TReal 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)
 {
       aStream<<*iName;
	   aStream.WriteInt32L(iNo);
	   aStream.WriteReal32L(iScore);
 }
 void CStudent::InternalizeL(RReadStream& aStream)
 {
	 delete iName;
	 iName=NULL;
     iName=HBufC::NewL(aStream,10);
	 
	 iNo=aStream.ReadInt32L();
	 iScore=aStream.ReadReal32L();
 }
 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=%f\n");
	_LIT(KNewLine,"\n");
	console->Printf(*iName);
    console->Printf(KNewLine);
	console->Printf(KNo,iNo);
	console->Printf(KScore,iScore);
	console->Printf(KNewLine);
}

 
///////////////////////////////////////////////////////////////////////////////////
//
//define Cstulist class to build list of student
//
///////////////////////////////////////////////////////////////////////////////////
 class CStuList:public CBase
 {
 public:
	static CStuList* NewL(TDesC& aFileName,CArrayFixFlat<CStudent>* aArray);
	static CStuList* NewLC(TDesC& aFileName,CArrayFixFlat<CStudent>* aArray);
 private:
	 CStuList();
	 void ConstructL(const TDesC& aFileName,CArrayFixFlat<CStudent>* aArray);
 public:
	 void WriteToFileL(RFs aFs);   //写文件
	 void ReadFromFileL(RFs aFs);  //读文件
     void BrowseList(); //浏览列表
	 void StuAppendL(const CStudent& aStudent); //添加数据
	
     void ExternalizeL(RWriteStream& aStream);  //外化
	 void InternalizeL(RReadStream&  aStream);  //内化
 private:
	 
	 CArrayFixFlat<CStudent>* iStudentArray;
	 TFileName iFileName;
};
///////////////////////////////////////////////////////////////////////////////////////
//
//implement CStuList
//
/////////////////////////////////////////////////////////////////////////////////////////
 CStuList* CStuList::NewL(TDesC& aFileName,CArrayFixFlat<CStudent>* aArray)
{
	CStuList* self=NewLC(aFileName,aArray);
	CleanupStack::Pop();
	return self;
}
CStuList* CStuList::NewLC(TDesC& aFileName,CArrayFixFlat<CStudent>* aArray)
{
	
	CStuList* self=new (ELeave)CStuList();
	CleanupStack::PushL(self);
    self->ConstructL(aFileName,aArray);
	return self;
}
void CStuList::StuAppendL(const CStudent& aStudent)
{
	iStudentArray->AppendL(aStudent);
}
void CStuList::BrowseList()
{
	
	_LIT(KEmpty,"List is empty!\n");
	TInt count;

	count=iStudentArray->Count();
	if(count==0)
	{
		console->Printf(KEmpty);
	}
	else
	{
	 for(TInt i=0;i<count;i++)
	 {
		(*iStudentArray)[i].Display();
		
	 }
	}//else
}

CStuList::CStuList()
{
}
void CStuList::ConstructL(const TDesC& aFileName,CArrayFixFlat<CStudent>* aArray)
{
   iFileName=aFileName;
   iStudentArray=aArray;
}
void CStuList::WriteToFileL(RFs aFs)
{
	RFileWriteStream OutStream; 
	User::LeaveIfError(OutStream.Replace(aFs,iFileName,EFileWrite));
	OutStream.PushL();
	ExternalizeL(OutStream);
//	OutStream<<*iStudentArray;
    OutStream.CommitL();
	OutStream.Pop();
	OutStream.Release();

}
void CStuList::ExternalizeL(RWriteStream& aStream)
{
	TInt32 numberOfelem;
	numberOfelem=iStudentArray->Count();
	aStream<<numberOfelem;
	for(TInt i=0;i<numberOfelem;i++)
	{
       (*iStudentArray)[i].ExternalizeL(aStream);
	}
}
void CStuList::ReadFromFileL(RFs aFs)
{
	RFileReadStream InStream;
	User::LeaveIfError(InStream.Open(aFs,iFileName,EFileRead));
	InStream.PushL();
	InternalizeL(InStream);
    
    InStream.Pop();
	InStream.Release();

}
void CStuList::InternalizeL(RReadStream& aStream)
{
    TInt32 numberOfelem;
	
	aStream>>numberOfelem;//写入数组元素个数
	iStudentArray->Reset();//清空数组
	BrowseList(); //浏览
	console->Getch();
	iStudentArray=new CArrayFixFlat<CStudent>(numberOfelem);
	for(TInt i=0;i<numberOfelem;i++)
	{
        CStudent* stu=CStudent::NewLC(aStream);//从文件读取
        iStudentArray->AppendL(*stu); //添加到数组中
		CleanupStack::Pop();
	}
}
//////////////////////////////////////////////////////////////////////////////
//
// 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 callExampleL() 
    {
   RFs iFs;//文件会话
   User::LeaveIfError(iFs.Connect());//connect to file server
   
   _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);
   

   CArrayFixFlat<CStudent> * array=new (ELeave) CArrayFixFlat<CStudent>(10);
   CStudent* stu1=CStudent::NewLC(Name1,1,80.0);
   CleanupStack::Pop(stu1);
   CStudent* stu2=CStudent::NewLC(Name2,2,70.7);  
   CleanupStack::Pop(stu2);
   CStudent* stu3=CStudent::NewLC(Name3,3,97.1);
   CleanupStack::Pop(stu3);
   CStudent* stu4=CStudent::NewLC(Name4,4,97.1);
   CleanupStack::Pop(stu4);
   //add three students to array using AppendL
   array->AppendL(*stu1);
   array->AppendL(*stu2);
   array->AppendL(*stu3);
 //  array->AppendL(*stu4);
   
   _LIT(KFileName,"c:\\stuList2.txt");
   TBuf<20> FileName(KFileName);
   CStuList* stuList=CStuList::NewLC(FileName,array);
   CleanupStack::Pop(stuList); 
   stuList->WriteToFileL(iFs);
   stuList->ReadFromFileL(iFs);
   stuList->BrowseList();
   //Close file server
   iFs.Close();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -