c++visual-c++newlinewchar

adding crlf to WCHAR * in VC++ 2005


this is VC++ 2005: How can I append a WCHAR * "firstText" and another WCHAR * "secondText" into another WCHAR * "thirdText" with a CRLF "\r\n" between them?

WCHAR firstText [100] = L"First line";

WCHAR secondText [100] = L"Second line";

WCHAR thirdText [500] = L"";

Your help is highly appreciated!


Solution

  • You can do the same as you would with non-W strings but using the wide string versions of the functions, e.g. (untested)

    int thirdTextMax = (sizeof(thirdText)/sizeof(thirdText[0]));
    swprintf(thirdText, thirdTextMax, L"%s\r\n%s", firstText, secondText);
    

    or

    int firstTextLen = wcslen(firstText);
    wcsncpy(thirdText, firstText, thirdTextMax);
    wcsncpy(thirdText + firstTextLen, L"\r\n", thirdTextMax - firstTextLen);
    wcsncpy(thirdText + firstTextLen + 2, secondText, thirdTextMax - firstTextLen - 2);
    

    (There are also the _s versions of these functions to be extra careful about overflowing the buffers but I can't remember if they're in VC2005 or not.)