您的位置:首页 > 运维架构 > Linux

[c++,linux] 接收可变参数并将其格式化输出的方法实现

2014-11-25 21:04 519 查看
实验目的:

学习并掌握 vsnprintf ,vsprintf, va_list 系列方法的使用通过 C 语言中的库函数的调用,

即其中的宏va_list 等定义来实现读取方法中所传入的可变参数。

在使用 va_list 的时候应该知道,

对于用来接收可变参数的方法中,系统都会为其在内存中开辟一块空间用来对用户传入的可变参数进行缓存,

而通过 va_list 创建的变量实质上就是指向这块缓冲区的指针。后续的操作就是辨别出每个参数的类型,

根据类型得知该类型的变量所占用的字节大小,

并根据字节的大小,依次向后移动指针,冲缓冲区中将用户输入的可变参数依次读取出来。

通过 va_start 宏来将调用者传入的可变参数根据调用者自定义的格式化样式进行格式化,

并对 一开始定义的va_list 变量进行初始化操作。

接下来,通常使用的方法就是,借助于 vsprintf,或是vsnprintf 函数将格式化好的数据转移到一块通过 字符指针指向的缓冲区中。

该换缓冲区的指针是通过方法的值-地址参数作为返回值进行返回给主调函数中的,

其中vsprint 与 vsnprintf 二者的主要区别是 前者并没有规定字符指针所指向的缓冲区的大小,容易造成越界访问或是溢出,

第二种方法通过传递的整数值来规定了指针访问空间的范围,从而避免了越界访问等一些列问题。

实验环境: Ubuntu,gcc

实验代码:vaFunc.c

编译命令: gcc vaFunc.c -o vaFunc

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

char buffer[101] ;

/**
Method :myPrintf
param1: fmt
this is used to pass the format pattern of output
just like the "%s %d" this kind of character stirng in printf
param2: ...
this is the so called the variable argument, it can receive
any kind of input parameters

function:
this method can return how many characters are received
and formatted by the vsprintf method ,and return the value
to the caller .
*/

int myPrintf (char *fmt , ... )
{
va_list vArgList ;
int counter ;

va_start(vArgList, fmt) ;
counter = vsprintf(buffer , fmt, vArgList) ;
va_end (vArgList) ;
return counter ;
}

/**
method: get_message
parameter:
param1: the value-address parameter , which is used to store
the return value from the get-message method

param2: size is used to make sure str's space will not be
out of range

param3: format pattern which is used to tell the method
in what kind of pattern the input messages are formatted

param4: "..." is the kind of parameter which can receive
arbitrary kind of arguments.

return: return value will be transfered by the first argument
char *str as the value-address type .
if everything goes correct , will return 0.
else an negtive value will be returned .
*/

int get_message (char *str , int size, char *fmt , ... )
{
va_list vArgList ; //vArgList is the pointer points to
//the argument's list space
int count ;
va_start (vArgList , fmt ) ;//we use the va_start method to
//initialize the vArgList as the
//user defined format pattern : fmt

//after we format the message , what to do next is
//transfer the message into a buffer ,here we use the vsprintf
//to transfer the formatted message into the character string buffer space

count = vsprintf(buffer, fmt , vArgList) ;
//return value from the vsprintf
//is used to record how many characters are formatted and
//transfered successfully , if something wrong return value will be an integer
//< 0
va_end(vArgList) ;

if (count >0 ||count< size )
return 0 ;
else
return -1 ;
}

//test two methods in main

int main ( int argc , char *argv )
{
int res ;

res = get_message(buffer,100 ,"%s,%d","hello inuyasha",20 ) ;
if (!res)
printf("\n%s\n\n" , buffer) ;

res = myPrintf("%d,%c,%s,%.2f", 1, 'a',"hello inuyasha",2.3432) ;
printf("there are %d characters in the parameters including the comma\n",res) ;

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐