有关fwrite语句的用法

2024-05-09 14:38
文章标签 fwrite 语句 用法

本文主要是介绍有关fwrite语句的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


一:函数名: 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.= 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个字节。



二:实例 可借鉴试读过程  转自:http://topic.csdn.net/t/20010530/11/139946.html

  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. }

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


三:数据块读写函数fread和fwite

  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. }

这篇关于有关fwrite语句的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/973668

相关文章

async与await 用法

一.async async 是 ES7 才有的与异步操作有关的关键字,和 Promise , Generator 有很大关联的。 1.async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数。 async function helloAsync() {return "hello world";}var asyn = helloAsync();console

ES6之Promise用法解析

一.Promise是什么 promise是一个对象,对象和函数的区别就是对象可以保存状态,函数不可以(闭包除外) 并未剥夺函数return的能力,因此无需层层传递callback,进行回调获取数据 主要用于异步计算可以将异步操作队列化,按照期望的顺序执行,返回符合预期的结果可以在对象之间传递和操作promise,帮助我们处理队列代码风格,容易理解,便于维护,多个异步等待合并便于解决 二.实例

MYSQL的流程控制语句

一、准备数据 create database ifTest;use ifTest;create table test(id int primary key auto_increment,typeId int not null comment '产品类型:1-普通商品 2-礼品卡 3-非卖品',productName varchar(50) not null comment '产品名称');

SQL语句之表的额外操作:create index,drop,alter,auto increment

文章目录 create indexdropalterAUTO INCREMENT create index 创建索引表,简单来说,就是为一个表A创建一个索引表a CREATE INDEX index_nameON table_name (column_name) 注释:“column_name” 规定需要索引的列。 如果是多个列 CREATE INDEX Person

github新手用法

目录 1,github账号注册2,github登录3,新建一个仓库4,往仓库里面写入东西或者上传东西5, 下载Git软件并安装6 ,获取ssh密钥7, 绑定ssh密钥8, 测试本地和github是否联通9,从GitHub远程网上仓库克隆文件到本地10,从本地上传同步文件到远程github网上仓库11,检查远程github网上仓库是否同步了 1,github账号注册 这是注册网页

关于template标签用法总结(含vue中的用法总结)

文章目录 一、html5中的template标签二、template标签操作的属性和方法三、vue中的template1、template标签在vue实例绑定的元素内部2、vue实例中的template属性 一、html5中的template标签 html中的template标签中的内容在页面中不会显示。但是在后台查看页面DOM结构存在template标签。这是因为templ

css中的@media用法总结

1、head标签中引入 //语法: @media mediatype and | not | only (media feature) { css-code; } //也可以针对不同的媒体使用不同的stylesheets: <!--<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="mysty

Sql 语句相关 忘得差不多了

ALTER TABLE t_plan ADD CONSTRAINT main_id_cons FOREIGN KEY (creator_id) REFERENCES t_employee(id);  添加外键; 给t_plan 添加 了 creator_id 到 t_employee 的id

Verilog基础语法——条件语句if-else与case

Verilog基础语法——条件语句case、if-else 写在前面一、if-else语句二、case语句2.1 case语句2.2 casez语句2.3 casex语句 写在后面 写在前面   在Verilog语法中,常用的条件语句有if-else语句和case语句,用于判断条件是否为真,并执行判断条件后面的表达式。 一、if-else语句   if-else语句的基本语法

【Python】语句与众所周知【自我维护版】

各位大佬好 ,这里是阿川的博客 , 祝您变得更强 个人主页:在线OJ的阿川 大佬的支持和鼓励,将是我成长路上最大的动力 阿川水平有限,如有错误,欢迎大佬指正 本篇博客是在之前的基础上进行的维护 目录 条件语句循环语句众所周知模板函数 条件语句 if elif else 程序只会选择满足的其中一个进入,当进入一个条件时,其他选择便不作考虑 if if