vsnprintf和gcc [英] vsnprintf and gcc

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

问题描述

我有以下语句:

vsnprintf(target, size - 1, "%ls_%ls", str16_1, str16_2);

为什么在gcc上失败?

Why does this fail on gcc?

我在Windows上使用这样的:

I used this on Windows like this:

vsnprintf(target, size - 1, "%S_%S", str16_1, str16_2);

并按预期工作。在gcc文档我发现%s是%ls的同义词,但我不能使用它。我也试过%S,但不工作。我在一个带有可变参数列表的函数中使用它。是否可能无法工作,因为我更改了我传递给va_start的格式变量?我必须搜索%S,并用格式变量中的%ls替换。

and it's working as expected. On gcc documentation I found that %S is synonym with %ls, but I must not use it. I tried also with %S, but is not working. I use this in a function with variable argument list. Is it possible to not work because I change the format variable that I pass to va_start? I must search %S and replace it with %ls in format variable.

函数是这样的:

void f(const char* format, ...){
  char* new_format = format with %S replaced with %ls;
  va_list argptr;
  va_start(args, format);
  vsnprintf(str, size-1, new_format, argptr);
}

我选中,new_format正确。

I checked and new_format is correct.

谢谢!

推荐答案

因为我在Mac上使用这个方法,我发现了一个解决方法:

Because I use this on Mac I found a work-around:

http://stackoverflow.com/questions/1420421/how-to-pass-on-a-variable-number-of-arguments-to-nsstrings-stringwithformat

似乎vsnprintf无法处理16位字符串。也许是因为wchar_t不是16位。

It seems that vsnprintf can't handle 16 bits string. Maybe because wchar_t isn't 16 bits.

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

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