SHFileOperation 使用字符串复制文件夹 [英] SHFileOperation copying folders using strings

查看:44
本文介绍了SHFileOperation 使用字符串复制文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SHFileOperationA 函数复制文件夹.这是我的代码.

I am trying to copy a folder by SHFileOperationA function. Here is my code.

int main()  {

    SHFILEOPSTRUCTA sf;
    int result;

    string source = "D:\\check\\folder4";
    string dest = "D:\\Documents\\test\\folder4";

    sf.pFrom = source.c_str( );
    sf.pTo = dest.c_str( );
    sf.wFunc = FO_COPY;
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT;

    result = SHFileOperationA(&sf);

    return 0;
}

我无法理解如何使 \0 附加的字符串两次.我试过这样的事情.

I am not able to understand that how to make the string appended by \0 twice. I tried something like this.

    string source = "D:\\check\\folder4\\0\\0";
    string dest = "D:\\Documents\\test\\folder4\\0\\0";

但是,它不起作用.我还尝试了更多的组合,但都没有奏效.请有人建议我如何解决这个问题?

But, it is not working. I have also tried few more combinations but none of them is working. Please can anyone suggest me how to solve this?

我可以通过像这样直接分配路径来解决问题:-

I can solve the problem by directly assigning the paths like this:-

    sf.pFrom = "D:\\check\\folder4";
    sf.pTo = "D:\\Documents\\test\\folder4";

问题解决了,但我的目的是使用字符串.请有人帮我解决这个问题.

and the problem gets solved but my intention is to make use of strings. Please can anybody help me with this.

另外,如果可能的话,谁能告诉我为什么直接分配字符串常量,即 sf.pFrom = "D:\\check\\folder4"; 正在工作并使用类似 sf.pFrom = source.c_str( ); 不起作用?

Also, if possible can anybody tell me why directly assigning the string constant i.e sf.pFrom = "D:\\check\\folder4"; is working and assigning using a string like sf.pFrom = source.c_str( ); is not working?

提前致谢.

推荐答案

std::stringstrlen(或类似的)来查找常量字符的结尾数组并仅分配所需数量的字符.所以它在第一个空值处停止并且不复制另一个空值.如果要覆盖它,则需要使用具有大小的构造函数,例如:

The std::string does strlen (or similar) to find the end of the constant char array and allocates only the required number of characters. So it stops on the first null and does not copy the other one. If you want to override this, you need to use constructor that takes size, like:

string source("D:\\check\\folder4\\0", 18);

请注意,第二个空字符是自动添加的,就像任何其他字符串一样.

Note that the second null character is added automatically just as with any other string.

或者像这样显式添加空字符(更好的解决方案):

Or add the null character explicitly like (way better solution):

std::string source = std::string("D:\\check\\folder4") + std::string(1, '\0');

std::string source = "D:\\check\\folder4";
source.append(1, '\0');

至于另一个问题,为什么使用常量字符数组有效:

As for the other question, why using constant character array works:

我相信,如果它有效,那么在常量字符数组之后的内存中的字节/字符为空只是一种纯粹的运气.

I believe that, if it works, it just a pure luck that the byte/character in memory after the constant character array is null.

您可以自己测试(在调试器中或通过打印值):

You can test this yourself (in debugger or by printing the values):

// this is the compiler generated null
(*(sf.pFrom + strlen(sf.pFrom))) == '\0'

// this is the following byte/character that should be luckily null
(*(sf.pFrom + strlen(sf.pFrom) + 1)) == '\0' 

如果在调试器中发现以下字符不为空,请确保在SHFileOperationA之前完成调试.在这种特殊情况下,它可能会失败/崩溃/发生任何事情(而通常不会),因为您单独调试的事实可能会使内存映像不同.

If you find that the following character is not null in debugger, make sure you finish debugging until the SHFileOperationA. As in that particular case it can fail/crash/whatever (while it normally does not) as the fact you are debugging alone may make the memory image different.

此外,您还没有具体说明它有效"的确切含义.你的意思是 SHFileOperationA 的结果(它是什么).或者应用程序崩溃/抛出,当它不起作用时.或者文件是否被复制.这可能有助于回答您.

Also you have not specified what you exactly mean by "it works". Do you mean result of SHFileOperationA (what is it). Or that the application crashes/throws, when it does not work. Or that the files are copied or not. This might help answering you.

这篇关于SHFileOperation 使用字符串复制文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆