UNIX到Windows:替代vsnprintf确定的长度? [英] Unix to Windows: Alternative to vsnprintf to determine length?

查看:573
本文介绍了UNIX到Windows:替代vsnprintf确定的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前转换我们的Linux图书馆之一的code到Windows DLL。

I am currently converting the code of one of our Linux libraries to a Windows DLL.

在这个库中我有一个函数,它发生在一个printf路的最后一个参数(格式字符串,
然后省略号)。在这个功能我用vsnprintf格式化提供的参数。
因为我想知道我是否能塞进最终串入一个小缓冲区,或者如果我不得不分配
记忆吧,我感兴趣的是确定格式化字符串的想成为长。

Within this library I have a function which takes the last parameters in a printf-way (format string, then ellipsis). Within this function I use vsnprintf to format the supplied arguments. Since I want to know whether I can cram the final string into a small buffer or if I'd have to allocate memory for it, I am interested in determining the "would-be-length" of the formatted string.

要做到这一点,我目前使用的vsnprintf这样的(由例如code,很明显):

To do this, I am currently using vsnprintf like this (made up example code, obviously):

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void foo(const char* fmt, ...)
{
   int len = 0;
   va_list ap;

   va_start(ap, fmt);
   len = vsnprintf(0, 0, fmt, ap);
   printf("len = %d\n", len);
   va_end(ap);
}

int main(void)
{
   foo("12345%s", "67890");
   exit(0);
}

此用法是由公开组基本规范问题6 包括:

vsnprintf(字符*限制S,为size_t N,为const char *限制格式,va_list的AP)

vsnprintf(char *restrict s, size_t n, const char *restrict format, va_list ap)

的[...] vsnprintf()[...]功能应等同于[...]的snprintf()。

的snprintf(字符*限制S,为size_t N,为const char *限制格式,...)

snprintf(char *restrict s, size_t n, const char *restrict format, ...)

如果n的值是零上调用的snprintf(),没有什么应该写的,应当退还,将被写入的n已经足够大,不包括终止空字节数,而s可能是一个空指针。

问题相比发生,因为我是用/分析关于编制本code对Windows的系统(Visual Studio 2010中)。编译器/分析仪给我的以下内容:

The problem arised as I was compiling this code on the Windows-System (Visual Studio 2010) with /analyze on. The compiler/analyzer gave me the following:

test.c以(11):警告C6309:参数'1'为null:这不符合函数'vsnprintf

test.c(11) : warning C6309: Argument '1' is null: this does not adhere to function specification of 'vsnprintf'

test.c以(11):警告C6387:'参数1'可能是'0':这不符合规范的功能'vsnprintf':行:7,8,10,11

test.c(11) : warning C6387: 'argument 1' might be '0': this does not adhere to the specification for the function 'vsnprintf': Lines: 7, 8, 10, 11

有一个快速浏览一下 MSDN条目vsnprintf 给了我这样的:

A quick look at the MSDN entry for vsnprintf gave me this:

如果的缓存的或的格式的就是 NULL ,或者如果计数小于或等于零,这些函数调用的参数无效处理程序,如参数验证所述。如果被允许继续执行,这些函数返回-1。

If buffer or format is NULL, or if count is less than or equal to zero, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return -1.

足够的好奇心,在上面的示例仍然工作在Windows上预期(即返回给我,将被写入字符的个数)。

Curious enough, the above sample works nonetheless on Windows "as expected" (i.e. it returns to me the count of the characters that would be written).

但因为我不希望这依赖于不确定的东西,我想知道是否有实现相同的更好,官方的方法,而不必希望这不会在未来的版本打破。

But since I don't want this to rely on something unspecified I'd like to know if there is a better, official way of achieving the same, without having to hope that this won't break in some future release.

感谢您的时间!

推荐答案

在给予解答汉斯帕桑特

该文件 _vscprintf 提供在Windows这一功能,
因此依托不确定的行为是没有必要的。

The documented _vscprintf provides this functionality on Windows, so relying on "unspecified behaviour" is not necessary.

这篇关于UNIX到Windows:替代vsnprintf确定的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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