C++中 sprintf函数的用法
来源:互联网 发布:淘宝体检中心登陆 编辑:程序博客网 时间:2024/06/05 16:04
C++中 sprintf函数的用法
1.常用方式
sprintf函数的功能与printf函数的功能基本一样,只是它把结果输出到指定的字符串中了,看个例子就明白了:
例:将”test 1 2”写入数组s中
#include<stdio.h>int main(int argc, char *avgv[]){ char s[40]; sprintf(s,"%s%d%c","test",1,'2'); /*第一个参数就是指向要写入的那个字符串的指针,剩下的就和printf()一样了 你可以比较一下,这是向屏幕输入*/ printf("%s%d%c","test",1,'2'); return 0;}
编译:
g++ sprinftest.cpp -o sprinftest && ./sprinftest
输出结果:
sprintftest12
sprintftest12
2.若”%s”等输出符在字符串中
例:补全字符串str的缺省内容
#include <iostream>#include <stdio.h>#include <cstring>int main(int argc, char *avgv[]){ char str[] = "hel%co wo%sd! sp%stf test%d"; char buf[strlen(str)]; sprintf(buf, str, 'l', "rl", "rin", 1); std::cout << "str = "<< buf << "\nlen = " << strlen(buf) << std::endl; return 0;}
编译:
g++ sprinftest.cpp -o sprinftest && ./sprinftest
输出结果:
str = hello world! sprintf test1
len = 27
这种形式也可以将多个字符值或字符串值赋值到字符串str中,有多少个输出符就后面就加多少个参数。
1 0
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C/C++ 语言中 sprintf() 函数的用法
- c语言中sprintf()函数的强大用法
- c语言中sscanf()和sprintf()函数的用法
- C 语言 sprintf函数的用法
- C++中 sprintf函数的用法
- loadrunner中sprintf函数的用法
- C++中 sprintf函数的用法
- C++中 sprintf函数的用法
- C\C++中sprintf的用法
- C/C++中sprintf的用法
- C 语言中sprintf的用法
- Mysql性能优化20条经验
- iOS调整系统屏幕亮度和音量
- 17
- python 正则表达式 重复匹配
- Iphone端relative absolute 出现问题
- C++中 sprintf函数的用法
- 一种减轻服务器负担并提高客户端间通讯效率的网络通讯设计[TCP/UDP合用] | 简单加密
- hadoop-hdfs学习2
- 在 iOS 中使用 Protocol Buffers
- LSH︱python实现局部敏感随机投影森林——LSHForest/sklearn(一)
- Mysql的几个坑(后续不断补充)
- 01-angularJs入门
- lock interupt
- python问号表达式