- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解C语言sscanf()函数、vsscanf()函数、vscanf()函数由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件:
1
|
#include <stdio.h>
|
sscanf()函数用于从字符串中读取指定格式的数据,其原型如下: 。
1
|
int
sscanf
(
char
*str,
char
* format [, argument, ...]);
|
【参数】参数str为要读取数据的字符串;format为用户指定的格式;argument为变量,用来保存读取到的数据.
【返回值】成功则返回参数数目,失败则返回-1,错误原因存于errno 中.
sscanf()会将参数str 的字符串根据参数format(格式化字符串)来转换并格式化数据(格式化字符串请参考scanf()), 转换后的结果存于对应的变量中.
sscanf()与scanf()类似,都是用于输入的,只是scanf()以键盘(stdin)为输入源,sscanf()以固定字符串为输入源.
【实例】从指定的字符串中读取整数和小写字母.
1
2
3
4
5
6
7
8
9
10
11
|
#include <stdio.h>
int
main(
void
)
{
char
str[100] =
"123568qwerSDDAE"
;
char
lowercase[100];
int
num;
sscanf
(str,
"%d %[a-z]"
, &num, lowercase);
printf
(
"The number is: %d.\n"
, num);
printf
(
"The lowercase is: %s."
, lowercase);
return
0;
}
|
输出结果:
1
2
|
The number is: 123568.
The lowercase is: qwer.
|
可以看到format参数有些类似正则表达式(当然没有正则表达式强大,复杂字符串建议使用正则表达式处理),支持集合操作,例如:
另外,format不仅可以用空格界定字符串,还可以用其他字符界定,可以实现简单的字符串分割(更加灵活的字符串分割请使用strtok())。例如:
1
2
|
sscanf
(
"2006:03:18"
,
"%d:%d:%d"
, a, b, c);
sscanf
(
"2006:03:18 - 2006:04:18"
,
"%s - %s"
, sztime1, sztime2);
|
C语言vsscanf()函数:字符串输入函数 头文件:
1
|
#include <stdio.h>
|
定义函数:
1
|
int
vsscanf(
const
char
* str,
const
char
* format,
va_list
ap);
|
函数说明:vsscanf()会将参数str 的字符串根据参数format 字符串来转换并格式化数据. 格式转换形式请参考附录C 或vprintf()范例.
返回值:成功则返回参数数目, 失败则返回-1, 错误原因存于errno 中. 。
C语言vscanf()函数:字符串格式化输入函数 头文件:
1
|
#include <stdio.h> #include <stdarg.h>
|
定义函数:
1
|
int
vscanf(
const
char
* format,
va_list
ap);
|
函数说明:vscanf()会将输入的数据根据参数format 字符串来转换并格式化数据. 格式转换形式请参考scanf(). 转换后的结果存于对应的参数内. va_list 用法请参考附录C 或vprintf()范例. 返回值成功则返回参数数目, 失败则返回-1, 错误原因存于errno 中.
最后此篇关于详解C语言sscanf()函数、vsscanf()函数、vscanf()函数的文章就讲到这里了,如果你想了解更多关于详解C语言sscanf()函数、vsscanf()函数、vscanf()函数的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我知道如何相应地从 printf 的实现中调用 vprintf 等。 但是如果我想在代码中动态创建一个参数列表,尤其是对于 vsscanf,我知道它们都是指针怎么办? 是否有一些合理的可移植方式可以将
我知道import CoreFoundation提供了像vscanf()这样的C函数。所以我可以使用 withUnsafePointer(&n){vscanf("%d",getVaList([COpa
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
我对 C 相当了解。我知道从 stdin 设置变量的唯一方法是使用 fscanf()/vscanf() 函数系列。 DOS 操作系统正在嵌入式市场中复苏。我使用的设备在编程上与 DOS 兼容,并使用具
我是一名优秀的程序员,十分优秀!