Fwrite和fprintf之间的区别(二)

2014-04-06 17:40:28 · 作者: · 浏览: 267

 

  二、 fprintf函数介绍

  #include

  int fprintf( FILE *stream, const char *format, ... );

  功能:根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件

  返回值:若成功则返回输出字符数,若输出出错则返回负值。

  #include

  #include

  FILE * stream;

  int main(void)

  {

  int i = 10;

  double fp = 1.5;

  char s[] = " this is a string";

  char c = '\n';

  stream = fopen("./fprintf.out","w");

  fprintf(stream, "%s%c", s, c);

  fprintf(stream, "%d\n", i);

  fprintf(stream, "%f\n", fp);

  fclose(stream);

  return 0;

  }

  结果:

  [root@f8s fprintf_test]# gcc fprintf_test.c -o fprintf_test

  [root@f8s fprintf_test]# ./fprintf_test

  [root@f8s fprintf_test]# cat fprintf.out

  this is a string

  10

  1.500000

  [root@f8s fprintf_test]#