Double null-terminated string

后端 未结 2 1873
野性不改
野性不改 2021-01-05 02:57

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

相关标签:
2条回答
  • 2021-01-05 03:14

    You cannot use CString for this purpose. You will need to use your own char[] buffer:

    char buf[100]; // or large enough
    strcpy(buf, "string to use");
    memcpy(buf + strlen(buf), "\0\0", 2);
    

    Although you could do this by only copying one more NUL byte after the existing NUL terminator, I would prefer to copy two so that the source code more accurately reflects the intent of the programmer.

    0 讨论(0)
  • 2021-01-05 03:36

    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.

    0 讨论(0)
提交回复
热议问题