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.
这篇关于双空终止字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:双空终止字符串
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01