printf 与 fprintf、sprintf函数、fscanf与scanf函数 的区别

   printf是标准输出流的输出函数,用来向屏幕这样的标准输出设备输出

而fprintf则是向文件输出,将输出的内容输出到硬盘上的文件或是相当于文件的设备上

  printf是有缓冲的输出,fprintf没有缓冲

  fprintf()传送输出到一个流中的函数

  原形:int fprintf(FILE *stream,const char *format[,argument,...])

  功能 <javascript:void(0)>:①向文件指针指向的文件输出ASCⅡ代码

  ②向显示器输出错误信息 <javascript:void(0)>

  使用形式:

  ①fprintf(文件指针,"输出格式",输出项系列);

  ②fprintf(stderr,"错误信息");

  使用实例:fprintf(stream,"%d %c %f",i,c,f);

  其中定义FILE *stream;

  int i; char c; float f;

  返回值:成功则返回输出的字节数,错误则返回EOF。

  头文件:stdio.h

  ===================================================

  sprintf()格式化输出到数组的函数

  原形∶int sprintf(buf,format,arg_list)

  其中char *buf;

  功能∶把arg_list参量的值按format的格式输出到buf中。

例∶sprintf(buffer,"相对扇区号∶%s(其中∶柱面号为%d/磁头号为%d/扇区号为%d)",string,cyls,head,sector);

  其中char buffer[100];

  char *string="  ";

  int cyls,head,sector;

  返回值∶实际写入数组的字符个数。

  头文件∶stdio.h

  sprintf 是个变参函数,定义如下:

  int sprintf(char *buffer,const char *format [, argument] ... );

  除了前两个参数类型固定外,后面可以接任意多个参数。而它的精华,显然就在第二个参数:

  格式化字符串上。

  printf 和sprintf 都使用格式化字符串来指定串的格式,在格式串内部使用一些以“%”开头的

  格式说明符(format specifications)来占据一个位置,在后边的变参列表中提供相应的变量,最终

  函数就会用相应位置的变量来替代那个说明符,产生一个调用者想要的字符串。

  格式化数字字符串

  sprintf 最常见的应用之一莫过于把整数打印到字符串中....

  格式化读写函数fscanf和fprintf

  fscanf函数,fprintf函数与前面使用的scanf和printf 函数的功能相似,都是格式化读写函数。 两者的区别在于 fscanf 函数和fprintf函数的读写对象不是键盘和显示器,而是磁盘文件。这两个函数的调用格式为: fscanf(文件指针,格式字符串,输入表列); fprintf(文件指针,格式字符串,输出表列); 例如:

  fscanf(fp,"%d%s",&i,s);

  fprintf(fp,"%d%c",j,ch);

[例1]  

    #include<stdio.h>
  struct stu
  {
  char name[10];
  int num;
  int age;
  char addr[15];
  }boya[2],boyb[2],*pp,*qq;
  main()
  {
  FILE *fp;
  char ch;
  int i;
  pp=boya;
  qq=boyb;
  if((fp=fopen("stu_list","wb+"))==NULL)
  {
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);
  }
  printf("\ninput data\n");
  for(i=0;i<2;i++,pp++)
  scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);
  pp=boya;
  for(i=0;i<2;i++,pp++)
  fprintf(fp,"%s %d %d %s\n",pp->name,pp->num,pp->age,pp->
  addr);
  rewind(fp);
  for(i=0;i<2;i++,qq++)
  fscanf(fp,"%s %d %d %s\n",qq->name,&qq->num,&qq->age,qq->addr);
  printf("\n\nname\tnumber age addr\n");
  qq=boyb;
  for(i=0;i<2;i++,qq++)
  printf("%s\t%5d %7d %s\n",qq->name,qq->num, qq->age,
  qq->addr);
  fclose(fp);
  }

[例2][ 对比fread和fwrite ]      

// crt_fread.c
// This program opens a file named FREAD.OUT and
// writes 25 characters to the file. It then tries to open
// FREAD.OUT and read in 25 characters. If the attempt succeeds,
// the program displays the number of actual items read.
#include <stdio.h>
int main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;
   // Open file in text mode:
   if( fopen_s( &stream, "fread.out", "w+t" ) == 0 )
   {
      for ( i = 0; i < 25; i++ )
         list[i] = (char)('z' - i);
      // Write 25 characters to stream 
      numwritten = fwrite( list, sizeof( char ), 25, stream );
      printf( "Wrote %d items\n", numwritten );
      fclose( stream );
}
   else
      printf( "Problem opening the file\n" );
 if( fopen_s( &stream, "fread.out", "r+t" ) == 0 )
   {
      // Attempt to read in 25 characters 
      numread = fread( list, sizeof( char ), 25, stream );
      printf( "Number of items read = %d\n", numread );
      printf( "Contents of buffer = %.25s\n", list );
      fclose( stream );
   }
   else
      printf( "File could not be opened\n" );
}
Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb
      printf( "File could not be opened\n" );
}

参考网站:http://blog.csdn.net/o_sun_o/article/details/8272371

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值