const char* 到 TDeC16

发布于 2024-08-04 16:32:52 字数 283 浏览 9 评论 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();
}

非常感谢,

I have a const char* that specifies the file that I want to delete.
I want to use RF::Delete to delete a file which takes a TDesC16
as input argument. Does anyone know how to easily convert

RFs fs;
TUint err;

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

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

Many thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-08-11 16:32:52
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);

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

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);

I haven't actually compiled this code so it may need som TLC.

一笔一画续写前缘 2024-08-11 16:32:52

类似的事情:

_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

Something along these lines:

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

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

but check http://descriptors.blogspot.com

梦中的蝴蝶 2024-08-11 16:32:52

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

假设它是 7 位 ASCII,那么

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

大括号就在那里,因为 TFileName 是一个相当大的类(512 字节左右,IIRC),因此您在将一个放入堆栈时要稍微小心,并给它尽可能小的范围。您可以改为堆分配。

如果是 UTF-8,则还有更多工作要做,请查看 CnvUtfConverter 类中的 ConvertToUnicodeFromUTF8

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

Depends what the character encoding is of the string pFileToDelete. If you don't know, then you need to find out (or define it yourself).

Assuming that it's 7-bit ASCII, then

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

Braces are there just because TFileName is quite a large class (512 bytes or so, IIRC), so you want to be slightly wary about putting one on the stack, and give it the smallest scope possible. You could heap-allocate instead.

If it's UTF-8, then there is more work to do, check out ConvertToUnicodeFromUTF8 in class CnvUtfConverter.

It's usually better to define your filename as a descriptor literal in the first place, if you can.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文