• 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 }
    View Code

      (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
    ");
    15         exit(0);
    16     }
    17 
    18     for(int i=0;i<4;i++)
    19         printf("%d
    ",buffer[i]);
    20     return 0;
    21 }
    View Code

     执行结果:

    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
    ");
    28     }
    29     fclose (pFile);
    30     return 0;
    31 }
    View Code

      (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
    ",per.age,per.name);
    21     }
    22     return 0;
    23 }
    View Code

    执行结果:

  • 相关阅读:
    0101
    正则表达式 re模块
    经典算法>>mor-c3 / 删除排序
    网络编程
    面向对象>>类(三大特性:继承多态封装)>>反射,内置函数/方法,
    经典算法>冒泡 和二分法
    Apollo 5.0 障碍物行为预测技术
    一种新颖鲁棒的自动驾驶车辆换道轨迹规划方法
    自动驾驶中轨迹规划的探索和挑战
    Lattice Planner规划算法
  • 原文地址:https://www.cnblogs.com/xudong-bupt/p/3478297.html
Copyright © 2020-2023  润新知