http://www.cplusplus.com/reference/cstdio/vsnprintf/

int vsnprintf (char * s, size_t n, const char * format, va_list arg );
Write formatted data from variable argument list to sized buffer

Composes a string with the same text that would be printed if format was used on printf, but using the elements in the variable argument list identified by arg instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by s (taking n as the maximum buffer capacity to fill).

If the resulting string would be longer than n-1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.

Internally, the function retrieves arguments from the list identified by arg as if va_arg was used on it, and thus the state ofarg is likely to be altered by the call.

In any case, arg should have been initialized by va_start at some point before the call, and it is expected to be released byva_end at some point after the call.

s:  Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least n characters.

n:  Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null character.size_t  is an unsigned integral type.formatC string that contains a format string that follows the same specifications as

format :  in printf (see printf for details).

argA :  value identifying a variable arguments list initialized with va_start.va_list is a special type defined in <cstdarg>.

Return Value

The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.
If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less than n, the string has been completely written.

/* vsnprintf example */
#include <stdio.h>
#include <stdarg.h> void PrintFError ( const char * format, ... )
{
char buffer[];
va_list args;
va_start (args, format);
vsnprintf (buffer,,format, args);
perror (buffer);//printf("%s",buffer);
va_end (args);
}

int main ()
{
FILE * pFile;
char szFileName[]="myfile.txt"; pFile = fopen (szFileName,"r");
if (pFile == NULL)
PrintFError ("Error opening '%s'",szFileName);
else
{
// file successfully open
fclose (pFile);
}
return ;
}

in eclipse with cygwin, compile get warning: implicit declaration of function ‘vsnprintf’.

http://cboard.cprogramming.com/c-programming/90483-snprintf-warning-error.html

"snprintf was not part of C90, it's a C99 addition or an extension to C90 available to certain compilers.

It looks like you may be using some variant of gcc, so try adding the flag -std=c99."

the above answer is right, in my make file, change c89 to c99, problem solved. also, just delete the origninate CPPUTEST_CFLAGS += -std=c89 also works. makefile is useful.

CPPUTEST_CFLAGS += -std=c99


to better understand , here is an application of snprintf.

static void failWhenNotAllExpectationsUsed(void)
{
char format[] = "Expected %d reads/writes but got %d";
char message[sizeof format + + ]; if (getExpectationCount == setExpectationCount)
return; snprintf(message, sizeof message, format, setExpectationCount,
getExpectationCount);
fail(message);
} static void fail(char *message )
{
failureAlreadyReported = ;
FAIL_TEXT_C(message);
}

vsnprintf的更多相关文章

  1. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数

    参考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html 总览 (SYNOPSIS) #include <stdio.h> int p ...

  2. 有关va_list和vsnprintf输出函数的问题

    va_list ap; //声明一个变量来转换参数列表 va_start(ap,fmt); //初始化变量 va_end(ap); //结束变量列表,和va_start成对使用 可以根据va_arg( ...

  3. sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析

    看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...

  4. vsnprintf和snprintf(vsnprintf就是为了支持va_list,实现对于sprint功能封装而做的)

    vsnprintf和snprintf是C语言printf家族函数的成员,相关函数列表如下: #include <stdio.h> int printf(const char *format ...

  5. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换

    总览 (SYNOPSIS) #include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream ...

  6. 使用vsnprintf后链接错误及解决方法

    /home/merlin/swinstall/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib//../../../../a ...

  7. linux内核调试技术之自构proc

    1.简介 在上一篇中,在内核中使用printk可以讲调试信息保存在log_buf缓冲区中,可以使用命令 #cat /proc/kmsg  将缓冲区的数区的数数据打印出来,今天我们就来研究一下,自己写k ...

  8. class.c 添加中文注释(3)

    int class_device_register(struct class_device *class_dev) { /* [cgw]: 初始化一个struct class_device */ cl ...

  9. centos 7.0 编译安装php 7.0.3

    php下载页面 http://cn2.php.net/downloads.php 7.0.3多地区下载页面 http://cn2.php.net/get/php-7.0.3.tar.gz/from/a ...

随机推荐

  1. C#- 反射之 GetType()方法

    Type.GetType()在跨程序集反射时返回null的解决方法 在开发中,经常会遇到这种情况,在程序集A.dll中需要反射程序集B.dll中的类型.如果使用稍有不慎,就会产生运行时错误.例如使用T ...

  2. ups机制下停电提前关闭oracle数据库

    思路:在一个受ups保护的机器A1上写脚本,几分钟(如半分钟)ping不在ups保护的机器B,如果几次(如5次)ping不同,这时我们认为将要停电,此时脚本执行正常关闭受ups保护的机器上数据库的命令 ...

  3. [BZOJ 3209]花神的数论题

    一道简单的数位 dp 题 但是脑子里只有 __builtin_popcountll 了呢(自重) 看完题解后很快就理解了,而且有一种这么简单的题居然没想到做法真是不应该唉~的感觉 用 f[i] 表示 ...

  4. 《统计推断(Statistical Inference)》读书笔记——第1章 概率论

    第一章介绍了基本的概率论知识,以下是这一章的思维导图

  5. MFC CPtrLink的使用

    if (!m_SALink.IsEmpty()) { POSITION pos = m_SALink.GetHeadPosition(); for (int j = 0; j < m_SALin ...

  6. jQuery的attr与prop,attribute和property区别

    jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...

  7. [Eclipse] - 集成JBoss7热加载和自动发布

    使用Eclipse + JBoss开发时,总是要重启项目或JBoss,烦人.下面方法可以很简单的实现Eclipse + JBoss热加载和自动发布. 我的环境是JBoss 7.1.1 Final 1) ...

  8. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  9. java短信接口

    一.背景 从是Java一直想做一个跟生活联系特别紧密的东西,比如短信.邮箱.电话什么的一直是我感兴趣的,可是楞是当初没有头绪弄,恰巧今天公司在做一个 webrtc的视频会议的软件,刚好有短信这个需求, ...

  10. view坐标_ _ Android应用坐标系统全面详解

    转:http://blog.csdn.net/yanbober/article/details/50419117 1 背景 去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自 ...