[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
学习并掌握 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 ; }
相关文章推荐
- SQLSERVER自增字段和Oracle创建自增字段-ORACLE SEQUENCE方法的对比-并且用存储过程实现参数输出
- C++可变参数的实现方法
- Java方法传参和可变参数的个别说明,以及foreach的语法和编译后的实现
- Linux C variadic可变参数:va_list 在x86和x64下的区别与实现原理
- params 实现可变数目方法参数
- jsp页面加载时div中循环输出ul,span标签接收参数并调用js方法
- Python实现字符串格式化输出的方法详解
- (十)linux下用标准i/o实现格式化输出并且把时间每隔一秒输出到文件中
- Java实现ping功能的三种方法--windows(版本)---linux下请自行修改 【ping】参数 跟 正则验证
- C语言中的可变参数函数的浅析(以Arm 程序中的printf()函数实现为例) . 分类: HI3531 arm-linux-Ubuntu 2013-12-16 14:19 438人阅读 评论(0) 收藏
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
- Linux 源码系列之可变参数列表实现
- linux下make命令实现输出高亮的方法
- C标准库学习--可变参数函数的实现方法
- linux c程序中获取shell脚本输出的实现方法
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
- 可变参数的实现是通过数组的方法?
- C语言可变参数和格式化输出
- Linux下date命令,格式化输出,时间设置方法
- Linux下彩色进度条的实现(printf的格式化输出成彩色的,在linux下才有效)