0

我有一个 const char* 指定要删除的文件。我想使用 RF::Delete 删除以 TDesC16 作为输入参数的文件。有谁知道如何轻松转换

RFs fs;
TUint err;

const char *pFileToDelete = "c:\\myfile.txt";

if ( fs.Connect () == KErrNone )
{
    err = fs.Delete(pFileToDelete); 
    fs.Close();
}

非常感谢,

4

3 回答 3

2
RFs fs;
TUint err;
const char *pFileToDelete = "c:\\myfile.txt";
TPtrC8 filename8 = (const TText8*)pFileToDelete;
//ok, so we could use a TBuf or a TFileName, but we'd need to now 
//the size of the TBuf at compile time and 
//TFileNames should never be allocated on the stack due to their size. 
//Easier to use a HBufC.
HBufC* filename = HBufC::NewLC(filename8.Length());
//Copy will only do the right thing if the text in pFiletoDelete is 7-bit ascii
filename->Des().Copy(filename8);
if ( fs.Connect () == KErrNone ){        
    err = fs.Delete(*filename);
    fs.Close();
}
CleanupStack::PopAndDestroy(filename);

我实际上还没有编译这段代码,所以它可能需要 som TLC。

于 2009-09-05T06:47:38.820 回答
0

这些方面的东西:

_LIT(KMyFilename,"c:\\myfile.txt");
TPtrC filename(KMyFilename);

RFs fs;
TInt err =fs.Connect();
User::LeaveIfError(err);
err = fs.Delete(filename);
...

但检查http://descriptors.blogspot.com

于 2009-09-04T16:36:54.317 回答
-1

取决于字符串的字符编码是什么pFileToDelete。如果您不知道,那么您需要找出(或自己定义)。

假设它是 7 位 ASCII,那么

TPtr8 wrapper(pFileToDelete, User::StringLength(pFileToDelete));
{
    TFileName name;
    name.Copy(wrapper);
    error = fs.Delete(name);
}

大括号的存在只是因为 TFileName 是一个相当大的类(大约 512 字节,IIRC),所以你要稍微谨慎地把一个放在堆栈上,并给它尽可能小的范围。您可以改为堆分配。

如果是 UTF-8,那么还有更多工作要做,请ConvertToUnicodeFromUTF8在课堂上查看CnvUtfConverter

如果可以的话,通常最好首先将文件名定义为描述符文字。

于 2009-09-04T14:44:37.730 回答