vsnprintf
http://www.cplusplus.com/reference/cstdio/vsnprintf/
int vsnprintf (char * s, size_t n, const char * format, va_list arg );
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的更多相关文章
- printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数
参考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html 总览 (SYNOPSIS) #include <stdio.h> int p ...
- 有关va_list和vsnprintf输出函数的问题
va_list ap; //声明一个变量来转换参数列表 va_start(ap,fmt); //初始化变量 va_end(ap); //结束变量列表,和va_start成对使用 可以根据va_arg( ...
- sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析
看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...
- vsnprintf和snprintf(vsnprintf就是为了支持va_list,实现对于sprint功能封装而做的)
vsnprintf和snprintf是C语言printf家族函数的成员,相关函数列表如下: #include <stdio.h> int printf(const char *format ...
- printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换
总览 (SYNOPSIS) #include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream ...
- 使用vsnprintf后链接错误及解决方法
/home/merlin/swinstall/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_EABI/bin/../lib//../../../../a ...
- linux内核调试技术之自构proc
1.简介 在上一篇中,在内核中使用printk可以讲调试信息保存在log_buf缓冲区中,可以使用命令 #cat /proc/kmsg 将缓冲区的数区的数数据打印出来,今天我们就来研究一下,自己写k ...
- class.c 添加中文注释(3)
int class_device_register(struct class_device *class_dev) { /* [cgw]: 初始化一个struct class_device */ cl ...
- 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 ...
随机推荐
- 开发问题(C#)
1. 重新引入dll文件之后,显示黄色感叹号,可能的原因:dll文件受损或者dll版本与工程Framework版本不一致. 2. 错误 232 无法将文件“..\SES.Common\bin\Debu ...
- [cyber security][php]pfSense目录遍历漏洞分析
0×00 导言 pfSense是一个基于FreeBSD,专为防火墙和路由器功能定制的开源版本. 在本文中,我们将向大家介绍在pfSense的2.1.3以及更低版本中的CVE-2014-4690漏洞:对 ...
- 简单的鼠标可拖动div 兼容IE/FF
来源:http://www.cnblogs.com/imwtr/p/4355416.html 作者: 主要思路: 一个div,注册监听onmousedown事件,然后处理获取的对象及其相关值(对象高度 ...
- Python error: ascii’/'utf-8′ codec can’t decode byte 0xb8 in position 50: ord
字符串使用了费ascii编码的字符,也就是它代表的16进制的编码超过127. 解决这个问题可以使用下面的方法解决,其实就是设置默认的编码.python 2.x的默认编码是ascii,如果改为utf-8 ...
- Python 3 条件、循环和assert、pass、del
条件: if 条件: 语句块 elif: 语句块 else: 语句块 elif 表示 else if 这居然是合法的!!!1 < x < 2!!! >> ...
- linux下Nginx 安装配置
Nginx 安装 一.首先要安装 PCRE PCRE 作用是让 Ngnix 支持 Rewrite 功能. 1.下载 PCRE 安装包,下载地址: http://downloads.sourceforg ...
- POST内容太大的问题
背景: 最近项目进入尾声,需要做交付测试,发现CMS生成页面在反复修改保存多次后,由于页面存在动态加载js的缘故,使得页面的内容越来越大,最终出现页面没有办法保存(ajax post将页面的内容发送给 ...
- bzoj3822: 文学
Description 巨酱和主席是一对好朋友.他们都很喜欢读书,经常一起阅读相关领域书籍,进行系统的学习.一天主席列出了一份列表,里面共 p 本书,其中不乏<约翰克里斯多夫>,<名 ...
- const,static,volatile
1.static 作用 在C语言中,关键字static有三个明显的作用:1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.2). 在模块内(但在函数体外),一个被声明为静态的 ...
- web.xml 文件配置01
web.xml 文件配置01 前言:一般的web工程中都会用到web.xml,方便开发web工程.web.xml主要用来配置Filter.Listener.Servlet等.但是要说明的是web. ...