的swprintf()窄弦 [英] swprintf() with narrow strings

查看:279
本文介绍了的swprintf()窄弦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code以下行给出了垃圾与Visual Studio 2010:

The following line of code gives garbage with Visual Studio 2010:

swprintf(buf, L"Value is %s", "abcd");

然而,同样的code正常工作在Linux上。

However, the same code works fine on Linux.

通过试验,我可以使它由Visual Studio下,使用%S代替%S的工作。

By trial, I could make it work by using %S instead of %s under Visual Studio.

swprintf(buf, L"Value is %S", "abcd");

我想知道,如果这是在Visual Studio 2010中的错误还是我失去了一些东西。问候。

I am wondering if this is a bug in Visual Studio 2010 or am I missing something. Regards.

推荐答案

这是一个错误,虽然该行为是由设计。最初的Visual C ++实现宽字符串的printf和scanf函数$ P $的pdated其标准化C,在某些情况下的行为,从被C标准库规范需要什么样的偏离。

This is a "bug," though the behavior is by design. The initial Visual C++ implementation of the wide string printf and scanf functions predated their standardization in C, and in some cases the behavior deviates from what is required by the C Standard Library specification.

在C标准库规范中,%S %C 格式说明必须始终与配对字符数组或元素,而长度修改时,必须使用一个 wchar_t的提供数组或元素。

In the C Standard Library specification, a %s or %c format specifier must always be paired with a char array or element, and the l length modifier must be used when a wchar_t array or element is provided.

在Visual C ++实现的这些功能​​(文档)在%S %C 格式说明期望的自然宽度的相应参数。对于窄字符串的printf和scanf函数,一个字符指针或元素是必需的和广泛的字符串函数 wchar_t的指针或元件是必需的。要通过其他宽度的字符串,可以使用%S %C 格式说明。另外, ^ h 可用于长度修改明确指定该字符串参数是一个窄或宽字符串

In the Visual C++ implementation of these functions (documentation), the %s and %c format specifiers expect a corresponding argument of the "natural" width. For the narrow string printf and scanf functions, a char pointer or element is required and for the wide string functions a wchar_t pointer or element is required. To pass a string of the "other" width, the %S and %C format specifiers may be used. Alternatively, the h and l length modifiers may be used to explicitly specify that the string argument is a narrow or wide string.

在其他的优势,Visual C ++实现的这些功能​​使我们能够轻松地迁移旧code通过的 _TCHAR &LT映射> 。不幸的是,什么是标准化的东西已经在Visual C ++实现实施不同的(我不熟悉这里的历史;它可能是什么标准匹配的另一种实现方式。)

Among other advantages, the Visual C++ implementation of these functions made it possible to easily migrate old code to use Unicode strings via the _TCHAR mappings in <tchar.h>. It is unfortunate that what was standardized was different from what had already been implemented in the Visual C++ implementation (I am not familiar with the history here; it may be that what was standardized matched another implementation.).

这篇关于的swprintf()窄弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆