Visitors hit counter dreamweaver

fprintf 百度百科上面的解释

简介

  是C/C++中的一个格式化写—库函数;其作用是格式化输出到一个流/文件中;

  函数完整形式: int fprintf(FILE *stream,char *format [,argument])

编辑本段功 能

  传送格式化输出到一个文件中

编辑本段用 法

  #include <stdio.h>

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

  fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.

编辑本段返回值

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

/* Program to create backup of the
  AUTOEXEC.BAT file */
  #include <stdio.h>
  int main(void)
  {
  FILE *in, *out;
  if ((in = fopen("\\AUTOEXEC.BAT", "rt")) == NULL)
  {
  fprintf(stderr, "Cannot open input file.\n");
  return 1;
  }
  if ((out = fopen("\\AUTOEXEC.BAK", "wt")) == NULL)
  {
  fprintf(stderr, "Cannot open output file.\n");
  return 1;
  }
  while (!feof(in))
  fputc(fgetc(in), out);
  fclose(in);
  fclose(out);
  return 0;
  }
  举例用法:
  #include <stdio.h>
  #include <process.h>
  FILE *stream;
  void 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 );
  system( "type fprintf.out" );
  }
  屏幕输出:
  this is a string
  10
  1.500000

  

posted @ 2011-10-14 12:03  Jason Damon  阅读(578)  评论(0)    收藏  举报