草庐IT

c - printf 未知说明符 %S

coder 2024-06-09 原文

我正在尝试让示例代码正常工作,它使用以下几行:

   WCHAR cBuf[MAX_PATH];

    GetSharedMem(cBuf, MAX_PATH);

    printf("Child process read from shared memory: %S\n", cBuf);

获取共享内存:

__declspec(dllexport) VOID __cdecl GetSharedMem(LPWSTR lpszBuf, DWORD cchSize) 
{ 
    LPWSTR lpszTmp; 

    // Get the address of the shared memory block

    lpszTmp = (LPWSTR) lpvMem; 

    // Copy from shared memory into the caller's buffer

    while (*lpszTmp && --cchSize) 
        *lpszBuf++ = *lpszTmp++; 
    *lpszBuf = '\0'; 
}

奇怪的是,我在 printf 行中收到一个未知的说明符错误。 %S 是一个 MS 扩展,与 ANSI 不兼容,但我认为默认情况下会包含它。我如何打开它?

我正在使用 Microsoft Visual Studio,不确定如何检查我的选项

最佳答案

来自 http://www.globalyzer.com/gzserver/help/localeSensitiveMethods/formatting.htm#larges

不合格的字符串说明符(大 %S)

这些表显示了 Windows 和 ANSI 如何根据 %S 说明符和函数调用的样式(单个、通用或宽)处理参数:

    Windows function                Specifier    Parameter needs to be
    printf/sprintf (single/MBCS)    %S           wchar_t*
    _tprintf/_stprintf (generic)    %S           (don't use)
    wprintf/swprintf (wide)         %S           char*

    ANSI function                   Specifier    Parameter needs to be
    printf/sprintf (single/MBCS)    %S           wchar_t*
    wprintf/swprintf (wide)         %S           wchar_t*

Both ANSI and Windows treat %S basically as opposite of %s in terms of single byte or wide, which ironically means that Windows and ANSI again handle these specifiers differently.

Note that ANSI in essence always treats %S in the same way as %ls, in other words it is always assumed to be wide string.

Windows on the other hand treats %S differently based on the type of function call. For single byte function calls, %S acts like the wide %ls specifier, but for wide functions calls, %S acts like the single byte %hs specifier.

This specifier should not be used for Windows Generic calls. Since %S is the "opposite" of %s, the parameter would need to be wide if the _UNICODE flag is off, and single byte if the _UNICODE flag is on. The TCHAR generic type does not work this way, and there's not "anti-TCHAR" kind of datatype.

I tried the following in Visual C++ 2010:

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    WCHAR cBuf[MAX_PATH];
    TCHAR tBuf[MAX_PATH];

    wcsncpy_s(cBuf, L"Testing\r\n", MAX_PATH);
    _tcsncpy_s(tBuf, _T("Testing\r\n"), MAX_PATH);

    printf("%S", cBuf); // Microsoft extension
    printf("%ls", cBuf); // works in VC++ 2010
    wprintf(L"%s", cBuf); // wide
    _tprintf(_T("%s"), tBuf); // single-byte/wide

    return 0;
}

设置:

/ZI/nologo/W3/WX-/Od/Oy-/D "WIN32"/D "_D​​EBUG"/D "_CONSOLE"/D "_UNICODE"/D "UNICODE"/Gm/EHsc/RTC1/GS/fp:precise/Zc:wchar_t/Zc:forScope/Yu"StdAfx.h"/Fp"Debug\TestWchar.pch"/Fa"Debug\"/Fo"Debug\"/Fd"Debug\vc100.pdb"/gd/analyze-/errorReport:队列

关于c - printf 未知说明符 %S,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7475452/

有关c - printf 未知说明符 %S的更多相关文章

  1. spring.profiles.active和spring.profiles.include的使用及区别说明 - 2

    转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev

  2. ruby-on-rails - 具有未知键和强参数的 Rails 哈希 - 2

    我有一个Rails应用程序,它在名为properties的字段中存储序列化哈希。虽然哈希键是未知的,所以我不知道有什么方法可以通过强参数实现这一点。谷歌搜索时,我发现了这个:https://github.com/rails/rails/issues/9454,但我想不出具体的解决方案。基本上,我的问题是:如何配置强参数以允许使用未知键的散列?感谢大家的帮助! 最佳答案 我最近遇到了同样的问题,我使用来自https://github.com/rails/rails/issues/9454的@fxn方法解决了它对于以properties

  3. ruby - Ruby 的 printf 参数是什么意思? - 2

    谁能帮我理解下面的表达方式?printf("%3d-%s\n",counter,name)该行打印类似这样的内容6-安装Adob​​e软件我查阅了资料并阅读了引用资料,但找不到简单的答案,我有点困惑。如果你能给我一个好的引用,请这样做。%3d好的,根据我的理解,%3d是字符数或空格数。请指出解释它的引用资料。%s\n我不知道这是做什么的。我猜\n是一个换行符或类似的东西,但通过查看输出,它似乎并不像那样工作。为什么counter和name变量用逗号分隔?通过查看输出,似乎%3d被替换为counter而%s\n被替换为名称。我不确定它是如何工作的,但我想了解它。

  4. ruby-on-rails - ActiveRecord:除非另有说明,否则在保存之前使所有文本字段都调用 strip - 2

    多年来,我在各种网站上遇到过各种问题,用户在字符串和文本字段的开头/结尾放置空格。有时这些会导致格式/布局问题,有时会导致搜索问题(即搜索顺序看起来不对,但实际上并非如此),有时它们实际上会使应用程序崩溃。我认为这会很有用,而不是像我过去所做的那样放入一堆before_save回调,向ActiveRecord添加一些功能以在保存之前自动调用任何字符串/文本字段上的.strip,除非我告诉它不是,例如do_not_strip:field_x,:field_y或类定义顶部的类似内容。在我去弄清楚如何做到这一点之前,有没有人看到更好的解决方案?明确一点,我已经知道我可以做到这一点:befor

  5. ruby - HTTP POST 上的 SSL 错误(未知协议(protocol)) - 2

    尝试通过SSL连接到ImgurAPI时出现错误。这是代码和错误:API_URI=URI.parse('https://api.imgur.com')API_PUBLIC_KEY='Client-ID--'ENDPOINTS={:image=>'/3/image',:gallery=>'/3/gallery'}#Public:Uploadanimage##args-Theimagepathfortheimagetoupload#defupload(image_path)http=Net::HTTP.new(API_URI.host)http.use_ssl=truehttp.verify

  6. ruby-on-rails - 更新 Ruby-Gems 时出现未知主机错误 - 2

    我正在尝试使用以下方法更新ruby​​gems:gemupdate--system但是我得到了错误:ERROR:Whileexecutinggem...(Gem::RemoteFetcher::UnknownHostError)nosuchname(https://rubygems.org/specs.4.8.gz). 最佳答案 不确定您安装的是哪个版本的ruby​​gems,但是可以通过三种方式修复:网络问题-稍后重试。您的版本已变砖无法更新,您需要以其他方式安装NOTE:RubyGems1.1and1.2haveproblems

  7. ruby - 如何从 Chef 说明书中的库访问当前节点? - 2

    我正在尝试为ChefRecipe编写一个库,以简化一些常见的搜索。例如,我希望能够在cookbook/libraries/library.rb中执行类似的操作,然后从同一Recipe中的Recipe中使用它:moduleExampledefself.search_attribute(attribute_name)returnsearch(:nodes,node[attribute_name])endend问题是,在Chef库文件中,node对象或search函数都不可用。似乎可以使用Chef::Search::Query.new().search(...)进行搜索,但我找不到任何可以访

  8. ruby-on-rails - 加载任何 yaml 文件时出现未知别名 - 2

    当我尝试在我的应用程序中加载任何yaml文件时,出现此错误:Psych::BadAlias:Unknownalias:default/Users/luizkowalski/.rbenv/versions/2.2.3/lib/ruby/2.2.0/psych/visitors/to_ruby.rb:385:in`visit_Psych_Nodes_Alias'/Users/luizkowalski/.rbenv/versions/2.2.3/lib/ruby/2.2.0/psych/visitors/visitor.rb:15:in`visit'/Users/luizkowalski/.

  9. ruby - 关于 Ruby << 运算符的说明 - 2

    我对Ruby很陌生,想知道运算符(operator)。当我用谷歌搜索这个运算符时,它说它是一个二进制左移运算符,给出了这个例子:awillgive15whichis11110000然而,它在这段代码中似乎不是“二进制左移运算符”:classTextCompressorattr_reader:unique,:indexdefinitialize(text)@unique=[]@index=[]add_text(text)enddefadd_text(text)words=text.splitwords.each{|word|doadd_word(word)}enddefadd_word(

  10. ruby-on-rails - wicked_pdf 在 unicode pdf 转换 (ruby) 上显示未知字符 - 2

    我正在尝试使用wicked_pdf(版本1.1)和wkhtmltopdf-binarygem从html页面创建pdf。我的html页面包含一个日历表情符号,无论我使用什么字体,它都能在浏览器中很好地显示unicode{font-family:'OpenSansEmoji',sans-serif;}@font-face{font-family:'OpenSansEmoji';src:url(data:font/truetype;charset=utf-8;base64,)format('truetype');}📅但是,当我尝试使用Rails控制台中gem的WickedPd

随机推荐