tags:

views:

356

answers:

4

I have a base wchar_t* and I'm looking to append another one onto the end. How do I do it? I cannot use deprecated functions as I am treating warnings as errors.

+3  A: 
 #include <wchar.h>

 wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n);

The wcsncat() function appends no more than the first n characters of the string pointed to by ws2 to the end of the string pointed to by ws1. If a NULL character appears in ws2 before n characters, all characters up to the NULL character are appended to ws1. The first character of ws2 overwrites the terminating NULL character of ws1. A NULL terminating character is always appended to the result, and if the objects used for copying overlap, the behavior is undefined.

ws1

Is the null-terminated destination string.

ws2

Is the null-terminated source string.

n

Is the number of characters to append.

Brendan
It has been declared deprecated.
Chad
+2  A: 

The most portable way to do this is wcsncat as mentioned above, but it sounds like you're committed to the "secure CRT" features of Visual C++ 2005 and later. (Only Microsoft has "deprecated" those functions.) If that's the case, use wcsncat_s, declared in string.h.

ChrisV
+4  A: 

Why not use a std::wstring in the first place:

wchar_t *ws1 = foo(), *ws2 = bar();
std::wstring s(ws1);
s += std::wstring(ws2);
std::wcout << s << std::endl;

If needed, std::wstring::c_str() gives you access to the result as a const wchar_t*.

Georg Fritzsche
or `const wchar_t * concatenation = s.c_str();` depending -- but yes the first thing I'd do is wrap them in a string type.
Steve Gilham
+1, people should use the STL where appropriate.
DaMacc
A: 
Mike Weller