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

📄 file.cpp

📁 《基于symbian os的手机开发与应用》
💻 CPP
字号:

//File.cpp

//Author:LiuLiPing
//version 1.0
//Date: 2006-2-14
//Example for showing you input words and if you want to exit press "Esc" 

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

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

// Function prototypes
 void callExampleL();
 void doExampleL();
 
//////////////////////////////////////////////////////////////////////////////
//
//       CWriteAndRead (definition)
//
// A class for Write and Read the word 
//
//////////////////////////////////////////////////////////////////////////////
 class CWriteAndRead:public CBase
 {
 public:
	 static CWriteAndRead* NewL(const TDesC& aFileName, const TDesC& aLetter);
	 static CWriteAndRead* NewLC(const TDesC& aFileName, const TDesC& aLetter);
 public:
	 void WriteToFile(RFs aFs);		//将输入的字符写入文件
	 void ReadFromFile(RFs aFs);	//从文件读取写入的字符
 private:
	void ExternalizeL(RWriteStream& aStream);  	//外化
	void InternalizeL(RReadStream&  aStream);	//内化
	void ConstructL(const TDesC& aFileName, const TDesC& aLetter);
	~CWriteAndRead();
 private:
	 HBufC* iLetter;		//用来保存当前字符
	 TFileName iFileName;	//文件名
 };

 
 //////////////////////////////////////////////////////////////////////////////
//
//       CWord (definition)
//
// A class for print and count the word number
//
//////////////////////////////////////////////////////////////////////////////

class CWord:public CBase
{
public:
	static CWord* NewL(TInt aMaxLength);
	static CWord* NewLC(TInt aMaxLength);
private:
	CWord(TInt aMaxLength);
	void ConstructL(TInt aMaxLength);
public:
	HBufC* GetWord();			//得到字符
	TInt WordL(TChar aChar);	//
	void HBufCToZero();			//将成员变量iHBufC清零,并且将iLength设置为零
	~CWord();
private:
	TInt iLength;
	TInt iMaxLength;
	HBufC* iHBufC;
};

//////////////////////////////////////////////////////////////////////////////
//
//       CAOExample (definition)
//
//////////////////////////////////////////////////////////////////////////////

class CAOExample:public CActive
{
public:
	static CAOExample* NewL(CConsoleBase* aConsole);
	static CAOExample* NewLC(CConsoleBase* aConsole);
private:
	CAOExample();
	void ConstructL(CConsoleBase* aConsole);
public:
	void Start();
	void Clear();					//调用CWord中的HBufCToZero()
	void KeyPressInput(TChar aChar);//接收当前输入的字符,并进行一系列处理操作
	~CAOExample();
private:
	void RunL();
	void Cancel();
	void DoCancel();
private:
	CConsoleBase* iConsole;
	CWord* iCWord;

};
////////////////////////////////////////////////////////////////////////////////
//
//		CWriteAndRead implement
// 
////////////////////////////////////////////////////////////////////////////////
CWriteAndRead* CWriteAndRead::NewL(const TDesC& aFileName, const TDesC& aLetter)
{
	CWriteAndRead* self = NewLC(aFileName,aLetter);
	CleanupStack::Pop(self);
	return self;
}
CWriteAndRead* CWriteAndRead::NewLC(const TDesC& aFileName, const TDesC& aLetter)
{
	CWriteAndRead* self = new(ELeave) CWriteAndRead();
	CleanupStack::PushL(self);
	self->ConstructL(aFileName,aLetter);
	return self;
}
void CWriteAndRead::ConstructL(const TDesC& aFileName, const TDesC& aLetter)
{
	iLetter = aLetter.Alloc();	//分配空间且初始化成员变量iLetter
	iFileName = aFileName;
}
void CWriteAndRead::WriteToFile (RFs aFs)
{
	RFileWriteStream writeStream; 
	User::LeaveIfError(writeStream.Replace(aFs,iFileName,EFileWrite));	//打开文件
	writeStream.PushL();		//压栈
    ExternalizeL(writeStream);	//内化
	writeStream.CommitL();		//提交
	writeStream.Pop();			//弹栈
	writeStream.Release();		//释放资源
}
void CWriteAndRead::ReadFromFile (RFs aFs)
{
	RFileReadStream readStream;
	User::LeaveIfError(readStream.Open(aFs,iFileName,EFileRead));	//打开文件
	readStream.PushL();
	InternalizeL(readStream);	//外化
	readStream.Pop();
	readStream.Release();
}
void CWriteAndRead::ExternalizeL (RWriteStream& aStream)
{
	aStream<<*iLetter;	//将iLetter写入文件
}
void CWriteAndRead::InternalizeL (RReadStream& aStream)
{
	TPtr ptr = iLetter->Des();
	ptr.Zero();
	delete iLetter;				
	iLetter = NULL;
	iLetter = HBufC::NewL(aStream,1);	//从文件中读取当前字符
    console->Printf(*iLetter);			//打印当前字符
}

CWriteAndRead::~CWriteAndRead()
{
	delete iLetter;
	iLetter = NULL;
}
////////////////////////////////////////////////////////////////////////////////
//
//		CWord implement
// 
////////////////////////////////////////////////////////////////////////////////

CWord* CWord::NewL(TInt aMaxLength)
{
   CWord* self = NewLC(aMaxLength);
   CleanupStack::Pop(self);
   return self;
}
CWord* CWord::NewLC(TInt aMaxLength)
{
   CWord* self = new(ELeave) CWord(aMaxLength);
   CleanupStack::PushL(self);
   self->ConstructL(aMaxLength);
   return self;
}

void CWord::ConstructL(TInt aMaxLength)
{
   iHBufC = HBufC::NewL(aMaxLength);
}

CWord::CWord(TInt aMaxLength)
{
   iMaxLength = aMaxLength;
   iLength = 0;
}

TInt CWord::WordL(TChar aChar)
{
   TPtr ptr=iHBufC->Des();
   if(iLength<iMaxLength) 
   {
	  ptr.Append(aChar);		 
	  iLength++;
	  return 1;
   }
	else 
	  return 0;
}
HBufC* CWord::GetWord()
{
	return iHBufC;
}

void CWord::HBufCToZero()
{
   iHBufC->Des().Zero();			//Sets the length of the data to zero
   iLength = 0;
}

CWord::~CWord()
{
   delete iHBufC;
   iHBufC = NULL;
   iLength = 0;
   iMaxLength = 0;
}

////////////////////////////////////////////////////////////////////////////////
//
//		CAOExample implement
// 
////////////////////////////////////////////////////////////////////////////////

CAOExample* CAOExample::NewL(CConsoleBase* aConsole)
{
   CAOExample* self =  NewLC(aConsole);
   CleanupStack::Pop(self);
   return self;
}

CAOExample* CAOExample::NewLC(CConsoleBase* aConsole)
{
   CAOExample* self = new(ELeave) CAOExample();
   CleanupStack::PushL(self);
   self->ConstructL(aConsole);
   return self;
}

void CAOExample::ConstructL(CConsoleBase* aConsole)
{
   iConsole = aConsole;
   iCWord = CWord::NewL(1);
   CActiveScheduler::Add(this);
}

CAOExample::CAOExample():CActive(EPriorityStandard)
{
}
void CAOExample::Start()
{
   iConsole->Read(iStatus);
   SetActive();
}

void CAOExample::Clear()
{
	iCWord->HBufCToZero();
}

void CAOExample::KeyPressInput(TChar aChar)
{
	RFs iFs;
	User::LeaveIfError(iFs.Connect());		//建立文件连接
	_LIT(KFile,"c:\\myfile\\file.txt");		//定义文件名
	TBuf<20> filename(KFile);
	if(aChar == EKeyEscape)			//如果输入Esc
	{
		Cancel();
		CActiveScheduler::Stop();
	}
	if(aChar == EKeyEnter)		//如果输入Enter
	{
		_LIT(KEnter,"\n");
		TBuf<1> buf(KEnter);
    	CWriteAndRead* wr = CWriteAndRead::NewL(filename,buf);	//建立CWriteAndRead的指针wr
	   
		wr->WriteToFile(iFs);	//写入文件
		wr->ReadFromFile(iFs);	//从文件读取
		Clear();
	}
	else
	{
    	iCWord->WordL(aChar);
    	CWriteAndRead* wr = CWriteAndRead::NewL(filename,*(iCWord->GetWord()));	   
    	wr->WriteToFile(iFs);
    	wr->ReadFromFile(iFs);
	    Clear();
	}  
	iFs.Close();
    Start(); 
}

void CAOExample::RunL()
{
   KeyPressInput((TChar)iConsole->KeyCode());
}

void CAOExample::Cancel()
{
   DoCancel();
}

void CAOExample::DoCancel()
{
   iConsole->ReadCancel(); //Cancels any pending Read() operations
}

CAOExample::~CAOExample()
{
	delete iCWord;
	iCWord = NULL;
	iConsole = NULL;
}

GLDEF_C TInt E32Main()
{
	
   CTrapCleanup* cleanup=CTrapCleanup::New();	// Get cleanup stack
   TRAPD(error,doExampleL());					// callExampleL() should never leave.
   _LIT(KMsgPanicEpoc32ex,"EPOC32EX");
   __ASSERT_ALWAYS(!error,User::Panic(KMsgPanicEpoc32ex,error));
   delete cleanup;								// destroy the cleanup stack
   return 0;									// return
    }

////////////////////////////////////////////////////////////////////////////////
//
//		doExample() implement
// 
////////////////////////////////////////////////////////////////////////////////

void doExampleL()
{
	_LIT(KPressAnyKey,"[Press any key...OK]");
  	_LIT(KMsgExampleCode,"Symbian OS AOExample");
	console = Console::NewL(KMsgExampleCode,TSize(KConsFullScreen,KConsFullScreen));
	CleanupStack::PushL(console);		// Put console onto the cleanup stack.
	int error;
	TRAP(error,callExampleL());			//TRAPD(error,callExampleL());
	    if(error)
	{
		_LIT(KERROR,"error occured!\n");
		console->Printf(KERROR);
	}
	else{
      _LIT(KNOLEAVE,"\nNo Leave!\n");
		console->Printf(KNOLEAVE);
	}
	console->Printf(KPressAnyKey);
	console->Getch();
	CleanupStack::PopAndDestroy();		//Remove the cleanupstack and destroy
}

////////////////////////////////////////////////////////////////////////////////
//
//		callExample() implement
// 
////////////////////////////////////////////////////////////////////////////////

void callExampleL() 
    {
     CActiveScheduler* AS=new (ELeave)CActiveScheduler;	
	 CleanupStack::PushL(AS);
	 CActiveScheduler::Install(AS);	
	 CAOExample* activeObjectExample=CAOExample::NewL(console);			
	 activeObjectExample->Start();							
	 console->Printf(_L("If you want to exit please press\"Esc\"!\n"));
	 console->Printf(_L("Please input word!\n"));
	 CActiveScheduler::Start();		
	 CleanupStack::PopAndDestroy();
}

⌨️ 快捷键说明

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