介绍C/C++文件的操作函数

开发 后端
本文主要介绍的是fread函数和fwrite函数,C/C++的文件操作。从功能到调用形式再到举例说明,介绍的很详细,希望对你有帮助,一起来看。

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

1.函数功能

用来读写一个数据块。

2.一般调用形式

 

  1. fread(buffer,size,count,fp);  
  2. fwrite(buffer,size,count,fp); 

 

3.说明

(1)buffer:是一个指针,对fread来说,它是读入数据的存放地址。对fwrite来说,是要输出数据的地址。

(2)size:要读写的字节数;

(3)count:要进行读写多少个size字节的数据项;

(4)fp:文件型指针。

注意:1 完成次写操(fwrite())作后必须关闭流(fclose());

2 完成一次读操作(fread())后,如果没有关闭流(fclose()),则指针(FILE * fp)自动向后移动前一次读写的长度,不关闭流继续下一次读操作则接着上次的输出继续输出;

3 fprintf() : 按格式输入到流,其原型是int fprintf(FILE *stream, const char *format[, argument, ...]);其用法和printf()相同,不过不是写到控制台,而是写到流罢了。注意的是返回值为此次操作写入到文件的字节数。

如:

  1. int c = fprintf(fp, "%s %s %d %f", str1,str2, a, b) ; 

 

str1:10字节;str2: 10字节;a:2字节;b:8字节,c为33,因为写入时不同的数据间自动加入一个空格。

文件使用之后一定要关闭,否则将不能正确显示内容.fwrite:读入两个学生信息然后用fwrite存入文件

fread:用fread从文件中读出学生信息。

 

fwrite.c

 

  1. #include <stdio.h>  
  2. #define SIZE 2  
  3. struct student_type  
  4. {  
  5. char name[10];  
  6. int num;  
  7. int age;  
  8. char addr[10];  
  9. }stud[SIZE];  
  10. void save()  
  11. {  
  12. FILE *fp;  
  13. int i;  
  14. if((fp=fopen("stu_list","wb"))==NULL)  
  15. {  
  16. printf("cant open the file");  
  17. exit(0);  
  18. }  
  19. for(i=0;i<SIZE;i++)  
  20. {  
  21. if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)  
  22. printf("file write error\n");  
  23. }  
  24. fclose(fp);  
  25. }  
  26. main()  
  27. {  
  28. int i;  
  29. for(i=0;i<SIZE;i++)  
  30. {  
  31. scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);  
  32. save();  
  33. }  
  34. for(i=0;i<SIZE;i++)  
  35. {  
  36. printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);  
  37. }  

fread.c

 

  1. #include <stdio.h>  
  2. #define SIZE 2  
  3. struct student_type  
  4. {  
  5. char name[10];  
  6. int num;  
  7. int age;  
  8. char addr[10];  
  9. }stud[SIZE];  
  10. void read()  
  11. {  
  12. FILE *fp;  
  13. int i;  
  14. if((fp=fopen("stu_list","rb"))==NULL)  
  15. {  
  16. printf("cant open the file");  
  17. exit(0);  
  18. }  
  19. for(i=0;i<SIZE;i++)  
  20. {  
  21. if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)  
  22. printf("file write error\n");  
  23. }  
  24. fclose(fp);  
  25. }  
  26. main()  
  27. {  
  28.  
  29. int i;  
  30. read();  
  31. for(i=0;i<SIZE;i++)  
  32. {  
  33. printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);  
  34. printf("\n");  
  35. }  

 

希望对你有帮助。

【编辑推荐】

  1. C++必须明白的基础20条
  2. C++多态技术的实现和反思
  3. VC++如何将程序最小化到托盘
  4. C/C++中内存区域划分大总结
  5. 阶乘相关的算法及其C++实现
责任编辑:于铁 来源: 博客园
相关推荐

2010-02-01 10:32:01

C++文件操作

2010-03-26 11:00:55

Python嵌入CC++

2010-03-26 11:00:55

Python嵌入CC++

2010-01-25 17:55:38

C++头文件

2020-09-28 08:12:59

CC++时间

2010-02-03 15:52:55

C++ clock()

2010-03-24 12:45:00

Python 嵌入

2011-07-13 11:34:58

CC++时间函数

2011-07-15 01:29:39

C++析构函数

2011-07-20 17:16:50

C++重载函数

2010-01-21 14:28:03

C++静态成员函数

2010-01-27 17:16:52

C++构造函数

2010-01-15 15:52:18

CC++

2010-01-26 10:42:26

C++函数

2010-01-15 10:41:06

CC++

2010-02-05 16:18:41

C++流式文件操作

2011-07-20 13:40:09

拷贝构造函数

2010-01-19 13:43:59

C++函数

2016-10-20 16:07:11

C++Modern C++异步

2010-02-05 10:46:10

C++文件流
点赞
收藏

51CTO技术栈公众号