zoukankan      html  css  js  c++  java
  • fprintf

    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.BAT", "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
     
      格式化规定符
     
      %d 十进制有符号整数
     
      %u 十进制无符号整数
     
      %f 浮点数
     
      %s 字符串
     
      %c 单个字符
     
      %p 指针的值
     
      %e 指数形式的浮点数
     
      %x, %X 无符号以十六进制表示的整数
     
      %0 无符号以八进制表示的整数
     
      %g 自动选择合适的表示法
    能力决定舞台,业绩体现价值,财富回报才智! 不积跬步,无以至千里;不积小流,无以成江海! 千里之行,始于足下。改变现在,就是改变未来。改变未来,从现在开始。
  • 相关阅读:
    quagga源码学习--BGP协议的初始化
    Golang pprof heap profile is empty
    python requests 配置超时及重试次数
    SVN: bdb: BDB1538 Program version 5.3 doesn't match environment version 4.7
    OpenSSL Command-Line HOWTO
    树莓派保卫战--防止SSH暴力破解
    Mac OS X Tips
    Git Tips
    SQL分组多列统计(GROUP BY后按条件分列统计)
    Show Linux Package Sort By Size
  • 原文地址:https://www.cnblogs.com/general001/p/2272838.html
Copyright © 2011-2022 走看看