wen*_*eng 6 c++ string mfc shfileoperation
我需要将字符串格式化为双尾终止字符串才能使用SHFileOperation.
有趣的部分是我发现以下工作之一,但不是两个:
// Example 1
CString szDir(_T("D:\\Test"));
szDir = szDir + _T('\0') + _T('\0');
// Example 2
CString szDir(_T("D:\\Test"));
szDir = szDir + _T("\0\0");
//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);
Run Code Online (Sandbox Code Playgroud)
有没有人对此有所了解?
是否有其他方法可以追加双端字符串?
CString类本身对包含空字符的字符串没有问题.问题在于首先将空字符放入字符串中.第一个例子有效,因为它附加了一个字符,而不是一个字符串 - 它接受该字符,而不检查它是否为空.第二个示例尝试附加一个典型的C字符串,根据定义结束于第一个空字符 - 您实际上是附加一个空字符串.
您不能CString用于此目的。您将需要使用自己的char[]缓冲区:
char buf[100]; // or large enough
strcpy(buf, "string to use");
memcpy(buf + strlen(buf), "\0\0", 2);
Run Code Online (Sandbox Code Playgroud)
尽管您可以通过在现有 NUL 终止符之后再复制一个 NUL 字节来实现这一点,但我更愿意复制两个,以便源代码更准确地反映程序员的意图。