C++ 你如何将两个 wchar_t* 连接在一起?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1855956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:24:54  来源:igfitidea点击:

How do you concatenate two wchar_t* together?

c++

提问by Chad

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.

我有一个基地wchar_t*,我想在最后追加一个。我该怎么做?我不能使用已弃用的函数,因为我将警告视为错误。

回答by Georg Fritzsche

Why not use a std::wstringin the first place:

为什么不首先使用 a std::wstring

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*.

如果需要,std::wstring::c_str()您可以访问结果作为const wchar_t*.

回答by Brendan Lesniak

 #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 ws2to the end of the string pointed to by ws1. If a NULLcharacter appears in ws2before ncharacters, all characters up to the NULLcharacter are appended to ws1. The first character of ws2overwrites the terminating NULLcharacter of ws1. A NULLterminating character is always appended to the result, and if the objects used for copying overlap, the behavior is undefined.

wcsncat()函数将不超过 指向的字符串的前 n 个字符附加到 指向的字符串ws2的末尾ws1。如果NULL字符出现在字符ws2之前n,则该字符之前的所有NULL字符都将附加到ws1。的第一个字符ws2覆盖 的终止NULL字符ws1NULL结果总是附加一个终止字符,如果用于复制的对象重叠,则行为未定义。

ws1

ws1

Is the null-terminated destination string.

是以空字符结尾的目标字符串。

ws2

ws2

Is the null-terminated source string.

是以空字符结尾的源字符串。

n

n

Is the number of characters to append.

是要追加的字符数。

回答by ChrisV

The most portable way to do this is wcsncatas 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.

执行此操作的最便携方法wcsncat如上所述,但听起来您致力于 Visual C++ 2005 及更高版本的“安全 CRT”功能。(只有 Microsoft 已“弃用”这些函数。)如果是这种情况,请使用wcsncat_s在 string.h 中声明的 。

回答by Mike Weller

Using the wstrncat/wcsncatfunctions is good, but I think the best version of these safe string functions are the 'l' ones created by Open BSD, i.e. strlcatand wstrlcat. With the 'n' versions, you can end up with a string that doesn't have a null terminator so you can still have security issues. Also certain implementations will zero out the unused space in the buffer which can slow things down a bit.

使用wstrncat/wcsncat功能还是不错的,但是我觉得这些安全的字符串函数最好的版本是由开放BSD,即创建了“L”的人strlcatwstrlcat。对于 'n' 版本,您最终会得到一个没有空终止符的字符串,因此您仍然可能遇到安全问题。此外,某些实现会将缓冲区中未使用的空间归零,这可能会减慢速度。

The wikipedia page has some more information on these functions: Strlcpy et al.. The only problem is these are not in the standard libraries so you have to include the code in your project yourself.

维基百科页面有更多关于这些函数的信息:Strlcpy 等。. 唯一的问题是这些不在标准库中,因此您必须自己将代码包含在项目中。

Here's the source to a wstrlcatfunction:

这是一个wstrlcat函数的来源:

/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz = siz, truncation occurred.
 */
size_t wstrlcat(wchar_t *dst, const wchar_t *src, size_t siz)
{
        wchar_t *d = dst;
        const wchar_t *s = src;
        size_t n = siz;
        size_t dlen;

        /* Find the end of dst and adjust bytes left but don't go past end */
        while(n-- != 0 && *d != L'##代码##') {
                d++;
        }

        dlen = d - dst;
        n = siz - dlen;

        if (n == 0) {
                return(dlen + wcslen(s));
        }

        while(*s != L'##代码##')
        {
                if(n != 1)
                {
                        *d++ = *s;
                        n--;
                }
                s++;
        }

        *d = '##代码##';
        return(dlen + (s - src));        /* count does not include NUL */
}