C语言中的fprintf函数

  1. fprintf  
  2. 简介  
  3.   c/c++语言函数: fprintf  
  4. 功 能  
  5.   传送格式化输出到一个文件中  
  6. 用 法  
  7.   #include <stdio.h>  
  8.   int fprintf( FILE *stream, const char *format, ... );  
  9.   fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.  
  10. 返回值  
  11.   成功时返回转换的字节数,失败时返回一个负数.  
  12.   在LINUX/UNIX操系统中成功返回0,失败返回-1。并置errno值.  
  13. 程序例  
  14.   /* Program to create backup of the 
  15.   AUTOEXEC.BAT file */  
  16.   #include <stdio.h>  
  17.   int main(void)  
  18.   {  
  19.   FILE *in, *out;  
  20.   if ((in = fopen("//AUTOEXEC.BAT""rt")) == NULL)  
  21.   {  
  22.   fprintf(stderr, "Cannot open input file./n");  
  23.   return 1;  
  24.   }  
  25.   if ((out = fopen("//AUTOEXEC.BAK""wt")) == NULL)  
  26.   {  
  27.   fprintf(stderr, "Cannot open output file./n");  
  28.   return 1;  
  29.   }  
  30.   while (!feof(in))  
  31.   fputc(fgetc(in), out);  
  32.   fclose(in);  
  33.   fclose(out);  
  34.   return 0;  
  35.   }  
  36.   举例用法:  
  37.   #include <stdio.h>  
  38.   #include <process.h>  
  39.   FILE *stream;  
  40.   void main( void )  
  41.   {  
  42.   int i = 10;  
  43.   double fp = 1.5;  
  44.   char s[] = "this is a string";  
  45.   char c = '/n';  
  46.   stream = fopen( "fprintf.out""w" );  
  47.   fprintf( stream, "%s%c", s, c );  
  48.   fprintf( stream, "%d/n", i );  
  49.   fprintf( stream, "%f/n", fp );  
  50.   fclose( stream );  
  51.   system( "type fprintf.out" );  
  52.   }  
  53.   屏幕输出:  
  54.   this is a string  
  55.   10  
  56.   1.500000  
  57.   格式化规定符  
  58.   %d 十进制有符号整数  
  59.   %u 十进制无符号整数  
  60.   %f 浮点数  
  61.   %s 字符串  
  62.   %c 单个字符  
  63.   %p 指针的值  
  64.   %e 指数形式的浮点数  
  65.   %x, %X 无符号以十六进制表示的整数  
  66.   %0 无符号以八进制表示的整数  
  67.   %g 自动选择合适的表示法  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值