前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >fprintf函数的的用法matlab_fwrite函数的用法

fprintf函数的的用法matlab_fwrite函数的用法

作者头像
全栈程序员站长
发布2022-11-02 17:00:47
5970
发布2022-11-02 17:00:47
举报

fprintf

简介

  c/c++语言函数: fprintf

功 能

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

用 法

  #include <stdio.h>

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

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

返回值

  成功时返回转换的字节数,失败时返回一个负数.

  在LINUX/UNIX操系统中成功返回0,失败返回-1。并置errno值.

程序例

  /* 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

  格式化规定符

  %d 十进制有符号整数

  %u 十进制无符号整数

  %f 浮点数

  %s 字符串

  %c 单个字符

  %p 指针的值

  %e 指数形式的浮点数

  %x, %X 无符号以十六进制表示的整数

  %0 无符号以八进制表示的整数

  %g 自动选择合适的表示法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/180316.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • fprintf
  • 简介
  •   c/c++语言函数: fprintf
  • 功 能
  •   传送格式化输出到一个文件中
  • 用 法
  •   #include <stdio.h>
  •   int fprintf( FILE *stream, const char *format, … );
  •   fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.
  • 返回值
  •   成功时返回转换的字节数,失败时返回一个负数.
  •   在LINUX/UNIX操系统中成功返回0,失败返回-1。并置errno值.
  • 程序例
  •   /* 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
  •   格式化规定符
  •   %d 十进制有符号整数
  •   %u 十进制无符号整数
  •   %f 浮点数
  •   %s 字符串
  •   %c 单个字符
  •   %p 指针的值
  •   %e 指数形式的浮点数
  •   %x, %X 无符号以十六进制表示的整数
  •   %0 无符号以八进制表示的整数
  •   %g 自动选择合适的表示法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档