我正在使用以下代码来获取要保存到用于 tinyxml2 的文件路径:
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = (LPCWSTR)L"XML Files (*.xml)\0*.xml\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = (LPWSTR)szFileName;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = (LPCWSTR)L"xml";
GetSaveFileName(&ofn);
这工作正常并显示对话框但是当我在 szFileName 中查找返回值时,它似乎包含许多空字符:
szFileName 0x0026f5b0 "C" char[0x00000104]
[0x00000000] 0x43 'C' char
[0x00000001] 0x00 '\0' char
[0x00000002] 0x3a ':' char
[0x00000003] 0x00 '\0' char
[0x00000004] 0x5c '\\' char
[0x00000005] 0x00 '\0' char
[0x00000006] 0x61 'a' char
[0x00000007] 0x00 '\0' char
[0x00000008] 0x73 's' char
[0x00000009] 0x00 '\0' char
[0x0000000a] 0x64 'd' char
[0x0000000b] 0x00 '\0' char
[0x0000000c] 0x61 'a' char
[0x0000000d] 0x00 '\0' char
[0x0000000e] 0x73 's' char
[0x0000000f] 0x00 '\0' char
[0x00000010] 0x64 'd' char
[0x00000011] 0x00 '\0' char
[0x00000012] 0x61 'a' char
[0x00000013] 0x00 '\0' char
[0x00000014] 0x73 's' char
[0x00000015] 0x00 '\0' char
[0x00000016] 0x2e '.' char
[0x00000017] 0x00 '\0' char
[0x00000018] 0x78 'x' char
[0x00000019] 0x00 '\0' char
[0x0000001a] 0x6d 'm' char
[0x0000001b] 0x00 '\0' char
[0x0000001c] 0x6c 'l' char
当传递给 tinyxml2 时,最终会保存一个名称为 1 个字符长的文件,以及 szFileName 中的第一个字符。
为什么要在每个字符之后添加这些空字符?