fwrite函数的一般调用形式是什么?


本文摘自PHP中文网,作者青灯夜游,侵删。

fwrite函数的一般调用形式是“fwrite(buffer,size,count,fp);”;其中,buffer是准备输出的数据块的起始地址,size是每个数据块的字节数,count用来指定每写一次或输出的数据块,fp为文件指针。

fwrite() 是C 语言标准库中的一个文件处理函数,功能是向指定的文件中写入若干数据块,如成功执行则返回实际写入的数据块数目。该函数以二进制形式对文件进行操作,不局限于文本文件。

语法:

1

fwrite(buffer,size,count,fp)

参数:

  • buffer是准备输出的数据块的起始地址

  • size是每个数据块的字节数

  • count用来指定每写一次或输出的数据块

  • fp为文件指针。

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

注意

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

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

读写常用类型

(1)写int数据到文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#include <stdio.h>

#include <stdlib.h>

int main ()

{

  FILE * pFile;

  int buffer[] = {1, 2, 3, 4};

  if((pFile = fopen ("myfile.txt", "wb"))==NULL)

  {

      printf("cant open the file");

      exit(0);

  }

  //可以写多个连续的数据(这里一次写4个)

  fwrite (buffer , sizeof(int), 4, pFile);

  fclose (pFile);

  return 0;

}

(2)读取int数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include <stdio.h>

#include <stdlib.h>

 

int main () {

    FILE * fp;

    int buffer[4];

    if((fp=fopen("myfile.txt","rb"))==NULL)

    {

      printf("cant open the file");

      exit(0);

    }

    if(fread(buffer,sizeof(int),4,fp)!=4)   //可以一次读取

    {

        printf("file read error\n");

        exit(0);

    }

 

    for(int i=0;i<4;i++)

        printf("%d\n",buffer[i]);

    return 0;

}

执行结果:

1.png

5.读写结构体数据

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct{

    int age;

    char name[30];

}people;

 

int main ()

{

    FILE * pFile;

    int i;

    people per[3];

    per[0].age=20;strcpy(per[0].name,"li");

    per[1].age=18;strcpy(per[1].name,"wang");

    per[2].age=21;strcpy(per[2].name,"zhang");

 

    if((pFile = fopen ("myfile.txt", "wb"))==NULL)

    {

        printf("cant open the file");

        exit(0);

    }

 

    for(i=0;i<3;i++)

    {

        if(fwrite(&per[i],sizeof(people),1,pFile)!=1)

            printf("file write error\n");

    }

    fclose (pFile);

    return 0;

}

(2)读结构体数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct{

    int age;

    char name[30];

}people;

 

int main () {

    FILE * fp;

    people per;

    if((fp=fopen("myfile.txt","rb"))==NULL)

    {

      printf("cant open the file");

      exit(0);

    }

 

    while(fread(&per,sizeof(people),1,fp)==1)   //如果读到数据,就显示;否则退出

    {

        printf("%d %s\n",per.age,per.name);

    }

    return 0;

}

执行结果:

2.png

相关推荐:c语言教程视频

以上就是fwrite函数的一般调用形式是什么?的详细内容!

相关阅读 >>

C语言float类型小数点后有几位有效数字?

C语言中switch语句的case后能否是一个关系表达式

C语言编写爱心的代码是什么

C语言中\t是什么意思

C语言中==是什么意思?

C语言中“\n”是什么意思

C语言是高级语言吗?

C语言中的字符串比较函数是什么

C语言中,要求参加运算的数必须是整数的运算符是什么

C语言如何定义变量

更多相关阅读请进入《C语言》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...