Operações em arquivos
Dados do artigo
Artigo
Tradução:
Originado de File manipulation in Symbian C++
Por ivocalado
Última alteração feita por hamishwillee
em 16 Aug 2012
Este artigo contem diferentes funções que podem ser utilizadas para trabalhar com arquivos em Symbian C++. Tais funções podem ser categorizadas como funções de cópia, deleção, renomeação, recorte de arquivo etc...
Bibliotecas:
LIBRARY efsrv.lib, bafl.lib
Cabeçalho: FileOpers.h
#include <e32base.h>
class FileOpers
{
public:
static void CallDelete();
static void CallRename();
static void CallMove();
static void CallCopy();
static void CallFileExists();
//CFileMan
static TBool Delete(const TDesC &aName, TUint aSwitch=0);
//Rfs: Renames a Single File Or Directory
static TBool Rename(const TDesC &anOldName, const TDesC &aNewName);
//CFileName
static TBool Move(const TDesC &anOld, const TDesC &aNew);
//CFileMan
static TBool Copy(const TDesC &anOld,
const TDesC &aNew);//, TUint aSwitch=EOverWrite);
//BAFLUtils
static TBool TestFileExists(const TDesC &aFileName);
};
Fonte: FileOpers.cpp
#include <bautils.h>
#include <aknnotewrappers.h>
#include "FileOpers.h"
//Change as per your requirement
_LIT(KFilePathDelete,"\\System\\Test.txt");
_LIT(KFilePathOldRename,"\\System\\Test.txt");
_LIT(KFilePathNewRename,"\\System\\Test123.txt");
_LIT(KFilePathOldMove,"\\System\\Test.txt");
_LIT(KFilePathNewMove,"\\System\\Data\\");
_LIT(KFilePathOldCopy,"\\System\\Test.txt");
_LIT(KFilePathNewCopy,"\\System\\Data\\");
//FileExists()- Change as per your requirement
_LIT(KFileName,"\\System\\Test.txt");
void FileOpers::CallDelete()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePath;
filePath.Copy(KFilePathDelete);
if(FileOpers::Delete(filePath))
info->ExecuteLD(_L("File Deleted"));
else
info->ExecuteLD(_L("File Not Deleted"));
}
void FileOpers::CallRename()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldRename);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewRename);
if(FileOpers::Rename(filePathOld,filePathNew))
info->ExecuteLD(_L("File Renamed"));
else
info->ExecuteLD(_L("File Not Renamed"));
}
void FileOpers::CallMove()
{
CAknInformationNote* info = new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldMove);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewMove);
if(FileOpers::Move(filePathOld,filePathNew))
info->ExecuteLD(_L("File Moved"));
else
info->ExecuteLD(_L("File Not Moved"));
}
void FileOpers::CallCopy()
{
CAknInformationNote* info=new (ELeave) CAknInformationNote;
TBuf16<30filePathOld;
filePathOld.Copy(KFilePathOldCopy);
TBuf16<30filePathNew;
filePathNew.Copy(KFilePathNewCopy);
if(FileOpers::Copy(filePathOld,filePathNew))
info->ExecuteLD(_L("File Copied"));
else
info->ExecuteLD(_L("File Not Copied"));
}
void FileOpers::CallFileExists()
{
CAknInformationNote* info=new (ELeave) CAknInformationNote;
TBuf16<30fileName;
fileName.Copy(KFileName);
if(FileOpers::TestFileExists(fileName))
info->ExecuteLD(_L("File Exists"));
else
info->ExecuteLD(_L("File Doesn't Exist"));
}
TBool FileOpers::Delete(const TDesC &aName,TUint aSwitch)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result = fileMan->Delete(aName,aSwitch);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Rename(const TDesC &anOldName,const TDesC &aNewName)
{
RFs fs;
fs.Connect();
TInt result = fs.Rename(anOldName,aNewName);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Move(const TDesC &anOld, const TDesC &aNew)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result=fileMan->Move(anOld,aNew);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result == KErrNone;
}
TBool FileOpers::Copy(const TDesC &anOld,const TDesC &aNew)
{
RFs fs;
fs.Connect();
CFileMan* fileMan=CFileMan::NewL(fs);
CleanupStack::PushL(fileMan);
TInt result=fileMan->Copy(anOld,aNew);
CleanupStack::PopAndDestroy(fileMan);
fs.Close();
return result==KErrNone;
}
TBool FileOpers::TestFileExists(const TDesC &aFileName)
{
RFs fs;
fs.Connect();
TBool result = BaflUtils::FileExists(fs,aFileName);
fs.Close();
return result;
}
- Cole o arquivo de cabeçalho FileOpers.h no diretório inc.
- Cole o arquivo de fontes FileOpers.cpp no diretório src.
- Inclua o cabeçalho FileOpers.h no arquivoCYrAppUi.cpp, da seguinte forma:
#include "FileOpers.h"
- Finalmente chame as funções do cabeçalho incluído a partir da função HandleCommand() de CYrAppUi.Cpp , como:
void CYrAppUi::HandleCommandL( TInt aCommand )
{
switch( aCommand )
{
case EEikCmdExit:
case EAknSoftkeyExit:
Exit();
break;
case ELFileOperationsDeleteFile:
FileOpers::CallDelete();
break;
case ELFileOperationsRenameFile:
FileOpers::CallRename();
break;
case ELFileOperationsMoveFile:
FileOpers::CallMove();
break;
case ELFileOperationsCopyFile:
FileOpers::CallCopy();
break;
case ELFileOperationsFileExists:
FileOpers::CallFileExists();
break;
default:
break;
}
}

