Printf全家桶,fprintf、sprintf等


看代码碰到很多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$
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值