Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1253638
  • 博文数量: 160
  • 博客积分: 4132
  • 博客等级: 中校
  • 技术积分: 2086
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 21:56
文章分类

全部博文(160)

文章存档

2012年(25)

2011年(120)

2010年(15)

分类: C/C++

2011-02-15 11:16:52

一:函数名: fwrite

  功 : 写内容到流中

  用 :fwrite(buffer,size,count,fp);

  (1buffer:是一个指针,对fwrite来说,是要输出数据的地址。

  (2size:要写入的字节数;

  (3count:要进行写入size字节的数据项的个数;

  (4fp:目标文件指针。

  程序例:

  1. #include <stdio.h>
  2.   struct mystruct
  3.   {
  4.   int i;
  5.   char ch;
  6.   };
  7.   int main(void)
  8.   {
  9.   FILE *stream;
  10.   struct mystruct s;
  11.   if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
  12.   {
  13.   fprintf(stderr, "Cannot open output file.\n");
  14.   return 1;
  15.   }
  16.   s.i = 0;
  17.   s.ch = 'A';
  18.   fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */
  19.   fclose(stream); /* close file */
  20.   return 0;
  21.   }

fprintf的区别

fprintf(fp, "%d", buffer); 是将格式化的数据写入文件
fprintf(文件指针,格式字符串,输出表列); 

fwrite(&buffer, sizeof(int), 1, fp);是以二进位位方式写入文件
fwrite(数据,数据类型大小(字节数),写入数据的最大数量,文件指针); 

由于fprintf写入时,对于整数来说,一位占一个字节,比如1,占1个字节;10,占2个字节;100,占3个字节,10000,占5个字节
所以文件的大小会随数据的大小而改变,对大数据空间占用很大。
fwrite是按二进制写入,所以写入数据所占空间是根据数据类型来确定,比如int的大小为4个字节(一般32位下),那么整数10所占空间为4个字节,10010000所占空间也是4个字节。所以二进制写入比格式化写入更省空间。

因此,
对于1 2 3 4 5 6 7 8 9 0 十个整数,用fprintf写入时,占10个字节;而用fwrite写入时,占40个字节。
对于100 101 102 103 104 105 106 107 108 109 110 这十个整数,用fprintf写入时,占30个字节;而用fwrite写入时,占40个字节。
对于10000 10100 10200 10300 10400 10500 10600 10700 10800 10900 11000 这十个整数,用fprintf写入时,占50个字节;而用fwrite写入时,还是占40个字节。



二:实例 可借鉴试读过程  转自:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. FILE *stream;//, *stream2;
  5. FILE *stream2;
  6. void main(void)
  7. {
  8.   int numclosed;
  9.    char *list;
  10.    list= "这个程序由阳永红编写 ";
  11.     if((stream= fopen("data.txt ","r "))==NULL) //试图打开文件data.txt,如果该文件不存在,则自动创建
  12.        {
  13.           printf("试图打开'data.txt '\n");
  14.           printf(" 'data.txt '不存在\n ");
  15.           printf(" 'data.txt '被创建\n ");
  16.         }
  17. else
  18.      printf(" 'data.txt '被打开\n "); //以写入方式打开
  19.     if( (stream2=fopen("data.txt ","w+ "))==NULL)
  20.      printf( " 'data.txt '不存在\n " );
  21.     else
  22.         {
  23.             printf( " 'data.txt '成功被打开\n " );
  24.           fwrite(list,strlen(list),30,stream2);
  25.           printf( "写入数据成功\n ");
  26.         }
  27. if(stream!=NULL) //如果data.txt存在就会打开成功,则stream!=NULL,这时就关闭stream
  28. if(fclose(stream))
  29. printf("文件流stream被关闭\n " );
  30. numclosed=_fcloseall( ); //关闭所有打开的文件流,返回关闭的文件流个数
  31. printf("被关闭的文件流量: %u\n ", numclosed );
  32. _getch(); //按任意键后退出
  33. }

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


三:数据块读写函数freadfwite

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #define N 5
  6. struct student
  7. {
  8. char name[20];
  9. int num;
  10. int age;
  11. char addr[15];
  12. };
  13. int main()
  14. {
  15. /*数据块读写函数fread和fwite
  16. 读取数据块函数调用的一般形式为
  17. fread(buffer,size,count,fp)
  18. 写数据块函数调用的一般形式为
  19. fwrite(buffer,size,count,fp)
  20. 其中:
  21. buffer是一个指针,在fread函数中,它表示存入数据的首地址;
  22. 在fwrite函数中,它表示输出数据的首地址
  23. size表示数据块的字节数
  24. count表示要读写的数据块的块数
  25. fp表示文件指针
  26. fread(array,4,10,fp)
  27. 表示从fp所指的文件中,每次读4个字节(一个实数)送入实数数组array中,
  28. 连续读取10次,即读10个实数到数组array中
  29. 从键盘输入N个学生的数据,写入一个文件中,再读出这N个学生的数据显示到屏幕上
  30. */
  31. struct student stua[N],stub[N],*pp=stua,*qq=stub;
  32. FILE *fp;
  33.     int i;
  34. if((fp=fopen("c:\\happy.txt","w+"))==NULL)
  35. {
  36. printf("Cannot open file,press any key exit!\n");
  37. getch();
  38. exit(1);
  39. }
  40. printf("Iput data \n");
  41. for(i=0;i<N;i++,pp++)
  42. {
  43. printf("请输入第%d位学生的数据:",i+1);
  44. scanf("%s%d%d%s",pp->name,&pp->num,&pp->age,pp->addr);
  45. }
  46. pp=stua;
  47. fwrite(pp,sizeof(struct student),N,fp);//写N个学生的记录
  48. rewind(fp);
  49. fread(qq,sizeof(struct student),N,fp);//读N个学生的记录
  50. printf("\n\n name number age addr \n");
  51. for(i=0;i<N;i++,qq++)
  52. printf("%-10s%-6d%-5d%-15s\n",qq->name,qq->num,qq->age,qq->addr);
  53. fclose(fp);
  54. return 0;
  55. }


阅读(48281) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~