Printf全家桶
看代码碰到很多printf家族的函数,很多我也不知道什么意思,所以这里记录一下最简单的用法!
功能描述,感兴趣的可以看下
DESCRIPTION
The functions in the printf() family produce output according to a format as
described below. The functions printf() and vprintf() write output to stdout, the
standard output stream; fprintf() and vfprintf() write output to the given output
stream; sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character
string str.
The function dprintf() is the same as fprintf() except that it outputs to a file
descriptor, fd, instead of to a stdio stream.
The functions snprintf() and vsnprintf() write at most size bytes (including the
terminating null byte ('\0')) to str.
The functions vprintf(), vfprintf(), vdprintf(), vsprintf(), vsnprintf() are equiva‐
lent to the functions printf(), fprintf(), dprintf(), sprintf(), snprintf(), respec‐
tively, except that they are called with a va_list instead of a variable number of
arguments. These functions do not call the va_end macro. Because they invoke the
va_arg macro, the value of ap is undefined after the call.
一.printf
int printf(const char *format, ...);
常见的就是它了,以某种格式输出,格式有很多,这里就不举例了
二.fprintf
int fprintf(FILE *stream, const char *format, ...);
看到形参FILE *stream
你会想到什么?这文件描述符不就是用来读、写文件的吗。
fprintf的作用是:把格式化的内容输出到stream(文件)
举例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fp;
int i = 0;//其返回值是输入到fp的字符个数
fp = fopen ("file.txt", "w+");
i = fprintf(fp, "%s %d", "nihao", 2022);
printf("input %d character\n", i);
fclose(fp);
return 0;
}
book@100ask:~/code/feof$ ./test
input 10 character
book@100ask:~/code/feof$ cat file.txt
nihao 2022
三.dprintf
int dprintf(int fd, const char *format, ...);
The function dprintf() is the same as fprintf() except that it outputs to a file descriptor, fd, instead of to a stdio stream.
dprintf和fprintf功能一样,区别在于dprintf输出到文件描述符fb,而不是标准输入输出流。至于用法嘛,没搜到,反正也没遇到过,暂且放这儿吧!
猜测:看形参int fd
应该是用open
打开的,然后写进去
四.sprintf
int sprintf(char *str, const char *format, ...);
sprintf把格式化的内容输出到str
#include <stdio.h>
int main()
{
char str[80] = {0};
int i = 0;
i = sprintf(str, "%f", 1.23);
printf("input %d character\n", i);
puts(str);
return 0;
}
book@100ask:~/code/feof$ ./test
input 8 character
1.230000
五.snprintf
int snprintf(char *str, size_t size, const char *format, ...);
带size_t size
的当然是限制了输出字节啦,其他与sprintf无异
#include <stdio.h>
int main()
{
char *str = "ABCDEFG";
char buf[10] = {0};
int i = 0;
i = snprintf(buf, 5, "%s", str);//返回值是欲写入字符个数
printf("input %d character\n", i);
puts(buf);
return 0;
}
book@100ask:~/code/feof$ ./test
input 7 character
ABCD
六.vprintf
int vprintf(const char *format, va_list ap);
vprintf也只是标准输出,只不过通过va_list ap可以设置很多format
看下面代码就理解了
#include <stdio.h>
#include <stdarg.h>
void my_vprintf(char *format, ...)
{
va_list args;//不需要知道原理,会用就行
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main ()
{
//可以输出%c %s %d %f %x这五种
my_vprintf("%c %s %d %f %x\n", 'I', "have", 10, 1.2, 10);
//也可以只输出%s这一种
my_vprintf("%s\n", "oh!" );
return 0;
}
book@100ask:~/code/feof$ ./test
I have 10 1.200000 a
oh!
你要问我这有什么用,我也不知道,好像printf一样能实现…
七.vfprintf
int vfprintf(FILE *stream, const char *format, va_list ap);
带FILE *stream
的,把格式化的内容出到stream
#include <stdio.h>
#include <stdarg.h>
void my_vnprintf(FILE *stream, char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
}
int main ()
{
FILE *fp;
fp = fopen("a.txt", "w");
my_vnprintf(fp, "%c %s %d %f %x\n", 'I', "have", 10, 1.2, 10);
my_vnprintf(fp, "%s\n", "oh!" );
fclose(fp);
return 0;
}
book@100ask:~/code/feof$ ./test
book@100ask:~/code/feof$ cat a.txt
I have 10 1.200000 a
oh!
八.vdprintf
int vdprintf(int fd, const char *format, va_list ap);
这个也比较邪乎,随便找了一下没找到,看来带d的都比较难搞啊!我选择直接放弃
九.vsprintf
int vsprintf(char *str, const char *format, va_list ap);
比vprintf多了个s,形参多了个char *str
,想到什么了?
当然是多种格式输出到str中了
#include <stdio.h>
#include <stdarg.h>
char buf[80];
int my_vspfunc(char *format, ...)
{
va_list aptr;
int ret;
va_start(aptr, format);
ret = vsprintf(buf, format, aptr);
va_end(aptr);
return ret;
}
int main()
{
int i = 5;
float f = 1.23;
char str[50] = "ABCDE";
my_vspfunc("%d %f %s", i, f, str);
printf("%s\n", buf);
return 0;
}
book@100ask:~/code/feof$ ./test
5 27.000000 ABCDE
十.vsnprintf
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
vsnprintf限制大小为size_t size
的字符串输出到str
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
char buf[20];
void my_vsnprintf( const char * format, ... )
{
va_list args;
va_start (args, format);
vsnprintf (buf, 20, format, args);
va_end (args);
printf("%s",buf);
}
int main()
{
my_vsnprintf("my name is %s,my age is %d\n","bob",18);
return 0;
}
这里限制了20个,所以只输出了my name is bob,my a
book@100ask:~/code/feof$ ./test
my name is bob,my abook@100ask:~/code/feof$