5.9 fread和fwrite函数-二进制I/O

来源:互联网 发布:网络彩票开售 编辑:程序博客网 时间:2024/04/26 01:19
fread和fwrite用于任意类型数据的读写。函数原型如下:

#include <stdio.h>size_t fread(void *restrict ptr, size_t size, size_t nobj, FILE *restrict fp);size_t fwrite(const void *restrict ptr,size_t nobj, FILE *restrict fp);

返回值都一样:返回读或写的对象数。所谓对象可以是任意数据类型,包括基本数据类型(如char、int、long等)和使用struct自定义的数据类型。

参数:

ptr 读或写的起始指针;

size 指针尺度,也就是要读或者写对象的数据类型的尺寸,一般用sizeof(数据类型)取得其尺寸;

nobj 读或者写的对象数量;

fp 流对象指针(文件指针)


实例 x.5.9.1.c(把一个字符数组保存到文件里)

#include <stdio.h>int main(void){    char     chararr[] = {65,66,67,68,69,70,71};  /*要保存的字符*/    char     charfile[] = "/tmp/charfile";        /*存放路径*/    FILE     *fp;    size_t   arrsize = sizeof(char);               /*元素长度*/    size_t   arrlen  = (sizeof(chararr))/arrsize;  /*数组长度*//*以写打开流*/    if ((fp = fopen(charfile, "w")) == NULL) {        printf("fopen error for %s\n", charfile);        return 1;    }/*流写入*/    if (fwrite(chararr, arrsize, arrlen, fp) != arrlen) {        printf("fwrite error!");        return 2;    }    fflush(fp);    fclose(fp);    return 0;}

编译与执行:

ABCDEFG[root@localhost unixc]# echo "" > /tmp/charfile
[root@localhost unixc]# cc x.5.9.1.c
[root@localhost unixc]# a.out
[root@localhost unixc]# cat /tmp/charfile
ABCDEFG[root@localhost unixc]#

实例 x.5.9.2.c (从文件中读一组字符到数组)

#include <stdio.h>int main(void){    char     chararr[8];                          /*用来保存字符的数组*/    char     charfile[] = "/tmp/charfile";        /*字符文件的存放路径*/    FILE     *fp;    size_t   arrsize = sizeof(char);               /*元素长度*/    size_t   arrlen  = (sizeof(chararr))/arrsize;  /*数组长度*//*以读打开流*/    if ((fp = fopen(charfile, "r")) == NULL) {        printf("fopen error for %s\n", charfile);        return 1;    }/*流读取*/    arrlen = fread(chararr, arrsize, arrlen - 1, fp); /*取arrlen-1个字符*/                                                      /*因为后面还要加一个数组结束字符*/    if (arrlen < 1) {        printf("fread error!");        return 2;    }    chararr[arrlen] = '\0';  /*fread不会在缓冲区后面自动加null或者回车符*/                             /*在此加上'\0'方便下面用printf输出*/    printf("%s\n", chararr);    fclose(fp);    return 0;}

编译与执行:

[root@localhost unixc]# echo "123456789" > /tmp/charfile
[root@localhost unixc]# cc x.5.9.2.c
[root@localhost unixc]# a.out
1234567
[root@localhost unixc]#


实例 x.5.9.3.c(写一个结构体到文件)

#include <stdio.h>#define NAMELEN 4/*定义结构体Student*/typedef struct{    int    id;    char   name[NAMELEN];}Student;int main(void){    Student     stuarr[2]= {{1, "zlw"}, {2, "zll"}}; /*要写入结构的数组*/    char        stufile[] = "/tmp/stufile";          /*保存结构的文件路径*/    FILE        *fp;    size_t   stusize = sizeof(Student);               /*元素长度*/    size_t   arrlen  = (sizeof(stuarr))/stusize;      /*数组长度*//*以写打开流*/    if ((fp = fopen(stufile, "w")) == NULL) {        printf("fopen error for %s\n", stufile);        return 1;    }/*流写入*/    arrlen = fwrite(stuarr, stusize, arrlen, fp);    if (arrlen < 0) {        printf("fwrite error!");        return 2;    }    fflush(fp);    fclose(fp);    return 0;}

编译与执行:

[root@localhost unixc]# cc x.5.9.3.c -o savestu
[root@localhost unixc]# echo "123456789" > /tmp/stufile
[root@localhost unixc]# ./savestu
[root@localhost unixc]# cat /tmp/stufile
zlwzll[root@localhost unixc]#

分析:用cat /tmp/stufile打开文件,出现乱码,这正好说明结构体已经正确写入,而不是出错。因为结构体中的id成员并不是以字符方式保存的,而cat却解释为字符,所以就出现乱码。


实例 x.5.9.4.c(把结构体从文件中读出来)

#include <stdio.h>#define NAMELEN 4/*定义结构体Student*/typedef struct{    int    id;    char   name[NAMELEN];}Student;int main(void){    Student     stuarr[2];                           /*存放结构的数组*/    char        stufile[] = "/tmp/stufile";          /*保存结构的文件路径*/    FILE        *fp;    int         i;    size_t   stusize = sizeof(Student);               /*元素长度*/    size_t   arrlen  = (sizeof(stuarr))/stusize;      /*数组长度*//*以读打开流*/    if ((fp = fopen(stufile, "r")) == NULL) {        printf("fopen error for %s\n", stufile);        return 1;    }/*流读出*/    arrlen = fread(stuarr, stusize, arrlen, fp);    if (arrlen < 0) {        printf("fread error!");        return 2;    }    fclose(fp);/*输出结构体信息*/    for (i = 0; i < arrlen; i++)        printf("id:%d; name:%s\n", stuarr[i].id, stuarr[i].name);/*Student.name是一个字符串,保存的时候其结束标志'\0'也会保存到文件,读取的时候也会读出来*/        return 0;}

[root@localhost unixc]# echo "123456789" > /tmp/stufile
[root@localhost unixc]# cc x.5.9.3.c -o savestu
[root@localhost unixc]# ./savestu
[root@localhost unixc]# cc x.5.9.4.c -o showstu
[root@localhost unixc]# ./showstu
id:1; name:zlw
id:2; name:zll
[root@localhost unixc]#

分析:用上例写入的结构体信息作为输入文件,通过本例的程序显示保存在文件中的结构体信息。