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

📄 dtfile.cpp

📁 一个关于通信的源代码
💻 CPP
字号:
/*
============================================================================
 Name        : DTFile.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : DTFile.cpp - source file
============================================================================
*/

// INCLUDE FILES
#include "DTFile.h"

void test()
{
	_LIT(KHello, "hello");
	TPtrC helloptr(KHello);

	const wchar_t* a = reinterpret_cast<const wchar_t*>(helloptr.Ptr());
}

void CDTFile::ConstructL()
{
	iIsOpen = false;
	User::LeaveIfError(iRFs.Connect());
}

CDTFile::~CDTFile()
{
	delete iFileName;
	iFileName = 0;
	iReader.Close();
	iWriter.Close();
	iRFs.Close();
}

CDTFile::CDTFile()
{
}

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

TInt CDTFile::OpenForRead(const TDesC& aFileName)
{
	TInt reason = 0;
	iFileName = aFileName.AllocL();
	iMode = KDTRead;

	RFile file;
	reason = file.Open( iRFs, *iFileName, EFileRead );
	if(reason == KErrNone)
	{
		file.Size(iFileLength);
		file.Close();
	}
	else
		return reason;

	reason = iReader.Open(iRFs, *iFileName, EFileRead);
	if(reason == KErrNone)
	{
		iIsOpen = true;
	}
	else
	{
		iIsOpen = false;
	}
	return reason;
}

TInt CDTFile::OpenForWrite(const TDesC& aFileName)
{
	TInt reason = 0;
	iFileName = aFileName.AllocL();
	iMode = KDTWrite;

	reason = iWriter.Replace(iRFs, *iFileName, EFileWrite);
	if(reason == KErrNone)
	{
		iIsOpen = true;
	}
	else
	{
		iIsOpen = false;
	}
	return reason;
}

TInt CDTFile::Read(TDes8& aBuffer, TInt aLength)
{
	TRAPD(error, iReader.ReadL(aBuffer, aLength));
	if(error ==  KErrEof)
	{
		aLength = iFileLength - iReadLength;
		aLength = aLength>0?aLength:0;
		return aLength;
	}
	iReadLength += aLength;
	return aLength;
}

TInt CDTFile::Write(TDes8& aBuffer, TInt aLength)
{
	TRAPD(error, iWriter.WriteL(aBuffer, aLength));
	if(error == KErrNone) return aLength;
	return 0;
}

TBool CDTFile::isOpen()
{
	return iIsOpen;
}

TInt CDTFile::GetLength()
{
	return iFileLength;
}

HBufC* CDTFile::AnsiToUnicodeL( const TDesC8& aAnsi )
{
	return CnvUtfConverter::ConvertToUnicodeFromUtf8L(aAnsi);
}

void CDTFile::Close()
{
	if(iMode == KDTRead && isOpen() ) iReader.Close();
	if(iMode == KDTWrite && isOpen() ) iWriter.Close();
}

⌨️ 快捷键说明

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