📄 file.cpp
字号:
// File.cpp: implementation of the CFile class.
//
//////////////////////////////////////////////////////////////////////
#include "File.h"
#include <s32file.h>
#include <eikenv.h>
//#include "UserInfo.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFile::CFile()
{
}
CFile::~CFile()
{
}
void CFile::WriteUserInfoToFileByStream( TDes &aName, TDes &aPassword )
{
//创建会话
RFs iFession;
User::LeaveIfError(iFession.Connect());
//删除文件
iFession.Delete(_L("SiriusUser.str"));
//以写的方式创建流文件
//对应模拟器中路径:C:\Symbian\7.0s\Series60_v21_C\Epoc32\wins\c\testStream.str
RFileWriteStream writeStream;
User::LeaveIfError(writeStream.Create(iFession, _L("C:\\SiriusUser.str"), EFileWrite));
//压栈
writeStream.PushL();
//从EDIT获取用户输入的数据
//HBufC* pStrName = HBufC::NewL(30); //pStr->Des().Copy(_L("Data stream of file !"));
//HBufC* pStrPassword = HBufC::NewL(30);
//将用户输入的数据存放到pStr中
//TPtr pTprStrName(pStrName->Des()), pTprStrPassword(pStrPassword->Des());
//pTprStrName.Copy(aName);
//pTprStrPassword.Copy(aPassword);
//写数据长度到流文件
//写数据到流文件:外化
writeStream.WriteInt32L(aName.Length());
writeStream << aName;
writeStream.WriteInt32L(aPassword.Length());
writeStream << aPassword;
//writeStream.WriteL(pStr->Des());
//清除/释放空间
//delete pStrName;
//delete pStrPassword;
//提交
writeStream.CommitL();
//弹栈
writeStream.Pop();
//释放资源
writeStream.Release();
writeStream.Close();
//关闭会话
iFession.Close();
}
void CFile::ReadUserInfoFromFileByStream( CUserInfo &aUserInfo )
{
//创建会话
RFs iFession;
User::LeaveIfError(iFession.Connect());
RFileReadStream readStream;
//压栈
readStream.PushL();
//以读取流文件的方式打开流文件
//对应模拟器中路径:C:\Symbian\7.0s\Series60_v21_C\Epoc32\wins\c\testStream.str
if( KErrNone != readStream.Open(iFession, _L("C:\\SiriusUser.str"), EFileRead))
{
//若文件打开失败,则先关闭会话再返回
//弹栈
readStream.Pop();
readStream.Release();
iFession.Close();
return;
}
//打开流文件成功
//读取流长度
TInt nLen = 0, nLenth = 0;
nLen = readStream.ReadInt32L();
//判断流内容长度
if(nLen > 0)
{
//若流数据内容长度大于零,则申请空间
HBufC* pStrName = HBufC::NewL(nLen);
CleanupStack::PushL(pStrName);
//读取流数据内容:内化
readStream >> pStrName->Des();
aUserInfo.SetName( pStrName->Des() );
nLenth = readStream.ReadInt32L();
HBufC* pStrPassword = HBufC::NewL(nLenth);
CleanupStack::PushL(pStrPassword);
readStream >> pStrPassword->Des();
aUserInfo.SetPassword( pStrPassword->Des() );
//readStream.ReadL(pStr->Des(), nLen);
//显示读取到的内容
//CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
//informationNote->ExecuteLD(pStr->Des());
CleanupStack::PopAndDestroy(2);
//清理空间
// delete pStrName;
// delete pStrPassword;
}
//弹栈
readStream.Pop();
//释放
readStream.Release();
readStream.Close();
//关闭会话
iFession.Close();
}
void CFile::ReadNoteInfoFromFileByStream(CArrayVarFlat<CNoteInfo>* aNoteInfo)
{
_LIT(KNoteInfo, "C:\\SiriusNote.data");
RFs iFession;
User::LeaveIfError(iFession.Connect());
//RFs& Fession = CEikonEnv::Static()->FsSession();
RFileReadStream readStream;
readStream.PushL();
if(KErrNone != readStream.Open(iFession, KNoteInfo, EFileRead))
{
readStream.Pop();
readStream.Release();
iFession.Close();
return ;
}
TInt32 Length = 0;
Length = readStream.ReadInt32L();//读取记事信息的条数
for(TInt32 i=0; i<Length; ++i)
{
CNoteInfo* note = new(ELeave) CNoteInfo;
CleanupStack::PushL(note);
note->InternalizeL(readStream);//内化
aNoteInfo->AppendL(*note,sizeof(*note));
CleanupStack::Pop(note);
delete note;
}
// for(TInt32 i=0; i<Length; ++i)
// {
// CNoteInfo* user = new(ELeave) CNoteInfo;
// CleanupStack::PushL(user);
// TInt32 nLengthTitle = readStream.ReadInt32L();//标题长度
// if(nLengthTitle > 0)
// {
// HBufC* bufTile;
// bufTile = HBufC::NewL(nLengthTitle);
// readStream >> bufTile->Des();
// user->SetTitle(bufTile->Des());
// delete bufTile;
// }
// TInt nLengthContent = readStream.ReadInt32L();//内容的长度
// if(nLengthContent > 0)
// {
// HBufC* bufContent = HBufC::NewL(nLengthContent);
// readStream >> bufContent->Des();
// user->SetContent(bufContent->Des());
// delete bufContent;
// }
// aNoteInfo->AppendL(*user,sizeof(*user));
// CleanupStack::Pop(user);
// }
readStream.Pop();
readStream.Release();
readStream.Close();
iFession.Close();
}
void CFile::WriteNoteInfoToFileByStream(CArrayVarFlat<CNoteInfo>* aNoteInfo)
{
_LIT(KNoteInfo, "C:\\SiriusNote.data");
//建立会话
// RFs& Fession = CEikonEnv::Static()->FsSession();
RFs Fession;
User::LeaveIfError(Fession.Connect());
Fession.Delete(KNoteInfo);
RFileWriteStream WriteStream;
// CleanupStack::PushL(WriteStream);
User::LeaveIfError(WriteStream.Create(Fession,KNoteInfo,EFileWrite));
TInt32 nLengthCount = aNoteInfo->Count();
WriteStream.WriteInt32L(nLengthCount);//写进总长度
for(TInt i = 0;i < nLengthCount;i++)
{
CNoteInfo* iTempInfo = &( aNoteInfo->At(i) );
iTempInfo->ExternalizeL(WriteStream);
iTempInfo->Delete();
// delete iTempInfo;
// iTempInfo = NULL;
}
// CleanupStack::Pop(WriteStream);
WriteStream.CommitL();
WriteStream.Release();
WriteStream.Close();
Fession.Close();
aNoteInfo->Reset();
}
//void FileLoad();
//void FileSave();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -