fwrite fread

C语言 fread()与fwrite()函数说明与示例

1.作用

  读写文件数据块。

2.函数原型

  (1)size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

     其中,ptr:指向保存结果的指针;size:每个数据类型的大小;count:数据的个数;stream:文件指针

     函数返回读取数据的个数。

  (2)size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

       其中,ptr:指向保存数据的指针;size:每个数据类型的大小;count:数据的个数;stream:文件指针

     函数返回写入数据的个数。

3.注意

  (1)写操作fwrite()后必须关闭流fclose()。

  (2)不关闭流的情况下,每次读或写数据后,文件指针都会指向下一个待写或者读数据位置的指针。

4.读写常用类型

  (1)写int数据到文件

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int main ()
 4 {
 5   FILE * pFile;
 6   int buffer[] = {1, 2, 3, 4};
 7   if((pFile = fopen ("myfile.txt", "wb"))==NULL)
 8   {
 9       printf("cant open the file");
10       exit(0);
11   }
12   //可以写多个连续的数据(这里一次写4个)
13   fwrite (buffer , sizeof(int), 4, pFile);
14   fclose (pFile);
15   return 0;
16 }
复制代码

  (2)读取int数据

复制代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main () {
 5     FILE * fp;
 6     int buffer[4];
 7     if((fp=fopen("myfile.txt","rb"))==NULL)
 8     {
 9       printf("cant open the file");
10       exit(0);
11     }
12     if(fread(buffer,sizeof(int),4,fp)!=4)   //可以一次读取
13     {
14         printf("file read error\n");
15         exit(0);
16     }
17 
18     for(int i=0;i<4;i++)
19         printf("%d\n",buffer[i]);
20     return 0;
21 }
复制代码

 执行结果:

5.读写结构体数据

  (1)写结构体数据到文件

复制代码
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 typedef struct{
 5     int age;
 6     char name[30];
 7 }people;
 8 
 9 int main ()
10 {
11     FILE * pFile;
12     int i;
13     people per[3];
14     per[0].age=20;strcpy(per[0].name,"li");
15     per[1].age=18;strcpy(per[1].name,"wang");
16     per[2].age=21;strcpy(per[2].name,"zhang");
17 
18     if((pFile = fopen ("myfile.txt", "wb"))==NULL)
19     {
20         printf("cant open the file");
21         exit(0);
22     }
23 
24     for(i=0;i<3;i++)
25     {
26         if(fwrite(&per[i],sizeof(people),1,pFile)!=1)
27             printf("file write error\n");
28     }
29     fclose (pFile);
30     return 0;
31 }
复制代码

  (2)读结构体数据

复制代码
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 typedef struct{
 5     int age;
 6     char name[30];
 7 }people;
 8 
 9 int main () {
10     FILE * fp;
11     people per;
12     if((fp=fopen("myfile.txt","rb"))==NULL)
13     {
14       printf("cant open the file");
15       exit(0);
16     }
17 
18     while(fread(&per,sizeof(people),1,fp)==1)   //如果读到数据,就显示;否则退出
19     {
20         printf("%d %s\n",per.age,per.name);
21     }
22     return 0;
23 }
复制代码

执行结果:

  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count"></span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/u013035197">
                    <img src="https://profile.csdnimg.cn/2/1/9/3_u013035197" class="avatar_pic" username="u013035197">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/6.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/u013035197" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">无敌三角猫</a></span>
                                            </div>
                    <div class="text"><span>发布了270 篇原创文章</span> · <span>获赞 208</span> · <span>访问量 136万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://bbs.csdn.net/topics/395530736" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-messageboard">他的留言板
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值