Double null-terminated string(双空终止字符串)
问题描述
为了使用 SHFileOperation,我需要将字符串格式化为双空终止字符串.
I need to format a string to be double null-terminated string in order to use SHFileOperation.
有趣的部分是我发现以下工作之一,但不是两个:
Interesting part is i found one of the following working, but not both:
// Example 1
CString szDir(_T("D:\Test"));
szDir = szDir + _T(' ') + _T(' ');
// Example 2
CString szDir(_T("D:\Test"));
szDir = szDir + _T(" ");
//Delete folder
SHFILEOPSTRUCT fileop;
fileop.hwnd = NULL; // no status display
fileop.wFunc = FO_DELETE; // delete operation
fileop.pFrom = szDir; // source file name as double null terminated string
fileop.pTo = NULL; // no destination needed
fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT; // do not prompt the user
fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle = NULL;
fileop.hNameMappings = NULL;
int ret = SHFileOperation(&fileop);
有人对此有想法吗?
还有其他方法可以附加双终止字符串吗?
Is there other way to append double-terminated string?
推荐答案
CString 类本身对包含空字符的字符串没有问题.问题在于首先将空字符放入字符串中.第一个示例有效,因为它附加的是单个字符,而不是字符串 - 它按原样接受字符,而不检查它是否为空.第二个示例尝试附加一个典型的 C 字符串,根据定义,该字符串在第一个空字符处结束 - 您实际上是在附加一个空字符串.
The CString class itself has no problem with a string containing a null character. The problem comes with putting null characters into the string in the first place. The first example works because it is appending a single character, not a string - it accepts the character as is without checking to see if it's null. The second example tries appending a typical C string, which by definition ends at the first null character - you're effectively appending an empty string.
这篇关于双空终止字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:双空终止字符串


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30