printf
printf是一个格式化输出函数
函数原型:
int printf(const char *format, …);
常用输出类型
类型 | 含义 |
---|---|
%d | 整型 |
%x | 十六进制数 |
%f | 单精度浮点数 |
%s | 字符串 |
%p | 指针 |
了解更多:详细链接
fprintf
作用:将格式化信息输出到某个文件流中
函数原型
int fprintf(FILE * restrict stream, const char * restrict format, …);
事例:
fprintf(stdout, “Hello world!\n”);
fprintf(stdout, “today is %s\n”, "Monday");
sprintf
功能:格式化字符串,将格式化的数据写入字符串中
函数原型:
int sprintf(char * restrict str, const char * restrict format, …);
适用场合
- 整数转字符串
char buf[20];
sprintf(buf, "%d", 34);
- 字符串转十六进制
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char *a = "hellowords";
unsigned char buff[40];
int i, j;
for( i = 0, j = 0; i < strlen(a); i++, j += 2)
{
sprintf( (char*)buff + j, "%02x", a[i]);
}
cout << buff << endl;
for(int i = 0; i < strlen(a); i++)
{
printf("%02x", a[i]);
}
return 0;
}
- 数据库操作使用
//略