fwrite和fread函数的用法小结

xiaoxiao2021-02-28  110

 

fwrite和fread是以记录为单位的I/O函数,fread和fwrite函数一般用于二进制文件的输入输出。

[cpp] view plain copy print ? #include <stdio.h>  size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);  size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);   #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); 返回值:读或写的记录数,成功时返回的记录数等于nmemb,出错或读到文件末尾时返回的记录 数小于nmemb,也可能返回0。     fread和fwrite用于读写记录,这里的记录是指一串固定长度的字节,比如一个int、一个结构体或者一个定长数组。参数size指出一条记录的长度,而nmemb指出要读或写多少条记录,这些记录在ptr所指的内存空间中连续存放,共占size * nmemb个字节,fread从文件stream中读出size * nmemb个字节保存到ptr中,而fwrite把ptr中的size * nmemb个字节写到文件stream中。     nmemb是请求读或写的记录数,fread和fwrite返回的记录数有可能小于nmemb指定的记录数。例如当前读写位置距文件末尾只有一条记录的长度,调用fread时指定nmemb为2,则返回值为1。如果当前读写位置已经在文件末尾了,或者读文件时出错了,则fread返回0。如果写文件时出错了,则fwrite的返回值小于nmemb指定的值。下面的例子由两个程序组成,一个程序把结构体保存到文件中,另一个程序和从文件中读出结构体 fread和fwrite的例子程序如下:

[cpp] view plain copy print ? /* -------------------writerec.c--------------- */  #include <stdio.h>  #include <stdlib.h>  struct record {      char name[10];      int age;  };  int main(void)  {      struct record array[2] = {{"Ken", 24}, {"Knuth", 28}};      FILE *fp = fopen("recfile""w");      if (fp == NULL) {          perror("Open file recfile");          exit(1);      }      fwrite(array, sizeof(struct record), 2, fp);      fclose(fp);      return 0;  }    /* -------------------readrec.c----------------- */  #include <stdio.h>  #include <stdlib.h>  struct record {      char name[10];      int age;  };  int main(void)  {      struct record array[2];      FILE *fp = fopen("recfile""r");      if (fp == NULL) {          perror("Open file recfile");          exit(1);      }      fread(array, sizeof(struct record), 2, fp);      printf("Name1: %s\tAge1: %d\n", array[0].name, array[0].age);      printf("Name2: %s\tAge2: %d\n", array[1].name, array[1].age);      fclose(fp);      return 0;  }   /* -------------------writerec.c--------------- */ #include <stdio.h> #include <stdlib.h> struct record { char name[10]; int age; }; int main(void) { struct record array[2] = {{"Ken", 24}, {"Knuth", 28}}; FILE *fp = fopen("recfile", "w"); if (fp == NULL) { perror("Open file recfile"); exit(1); } fwrite(array, sizeof(struct record), 2, fp); fclose(fp); return 0; } /* -------------------readrec.c----------------- */ #include <stdio.h> #include <stdlib.h> struct record { char name[10]; int age; }; int main(void) { struct record array[2]; FILE *fp = fopen("recfile", "r"); if (fp == NULL) { perror("Open file recfile"); exit(1); } fread(array, sizeof(struct record), 2, fp); printf("Name1: %s\tAge1: %d\n", array[0].name, array[0].age); printf("Name2: %s\tAge2: %d\n", array[1].name, array[1].age); fclose(fp); return 0; } [plain] view plain copy print ? $ gcc writerec.c -o writerec  $ gcc readrec.c -o readrec   $ gcc writerec.c -o writerec $ gcc readrec.c -o readrec 发现生成的文件recfile不能直接打开。     原因:我们把一个struct record结构体看作一条记录,由于结构体中有填充字节,每条记录占16字节, 把两条记录写到文件中共占32字节。该程序生成的recfile文件是二进制文件而非文本文件,因为其 中不仅保存着字符型数据,还保存着整型数据24和28(在od命令的输出中以八进制显示为030和034)。 注意,直接在文件中读写结构体的程序是不可移植的,如果在一种平台上编译运行writebin.c程序, 把生成的recfile文件拷到另一种平台并在该平台上编译运行readbin.c程序,则不能保证正确读出 文件的内容,因为不同平台的大小端可能不同(因而对整型数据的存储方式不同),结构体的填充方式 也可能不同(因而同一个结构体所占的字节数可能不同,age成员在name成员之后的什么位置也可能不同)。 通过readrec程序读取文件recfile的内容,说明writerec程序的确记录成功写入recfile中。     从recfile读出的内容如下:

    Name1: Ken    Age1: 24     Name2: Knuth  Age2: 28 fwrite和fread的应用举例: 1.将一个字符串写入文件:

[cpp] view plain copy print ? char *str="hello,I am a test program!";  fwrite(str,sizeof(char),strlen(str),fp)   char *str="hello,I am a test program!"; fwrite(str,sizeof(char),strlen(str),fp) 2.将一个字符数组写入文件:

[cpp] view plain copy print ? char str[]={'a','b','c','d','e'};  fwrite(str,sizeof(char),sizeof(str),fp)   char str[]={'a','b','c','d','e'}; fwrite(str,sizeof(char),sizeof(str),fp)

3.将一个整型数组写入文件:     int a[]={12,33,23,24,12};     先计算数组元素个数nmemb,之后     fwrite(a,sizeof(int),nmemb,fp)     注:由于程序生成的文件是二进制文件而非文本文件,因此,不用机器,整数的表达不同,         所以无法直接打开生成文件。可通过fread函数检验数据是否写入文件。

转自:http://purpleroom.blog.sohu.com/133812394.html

转载请注明原文地址: https://www.6miu.com/read-60487.html

最新回复(0)