C++文件操作具体应用函数介绍

开发 后端
C++文件操作中有几个比较重要的应用函数,包括:fread();fwrite();等。我们在这里将会针对这些函数的应用方法进行一个详细的介绍。

C++编程语言应用方式灵活,可以被看做C语言的升级版本。我们可以通过这篇文章介绍的关于C++文件操作的相关方法来对这一计算机编程语言的相关应用技巧有一个初步的掌握,并从中加深对这一语言的认知程度。#t#

1.C++文件操作中的函数功能

用来读写一个数据块。

2.一般调用形式

fread(buffer,size,count,fp);

fwrite(buffer,size,count,fp);

3.说明

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

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

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

(4)fp:文件型指针。

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

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

3 fprintf() : 按格式输入到流,其原型是int fprintf(FILE *stream, const char *format[, argument, ...]);其用法和printf()相同,不过不是写到控制台,而是写到流罢了。注意的是返回值为此次操作写入到文件的字节数。如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从文件中读出学生信息。

 

 

  1. fwrite.c  
  2. #include <stdio.h> 
  3. #define SIZE 2  
  4. struct student_type  
  5. {  
  6. char name[10];  
  7. int num;  
  8. int age;  
  9. char addr[10];  
  10. }stud[SIZE];  
  11. void save()  
  12. {  
  13. FILE *fp;  
  14. int i;  
  15. if((fp=fopen("stu_list","wb"))==NULL)  
  16. {  
  17. printf("cant open the file");  
  18. exit(0);  
  19. }  
  20. for(i=0;i<SIZE;i++)  
  21. {  
  22. if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)  
  23. printf("file write error\n");  
  24. }  
  25. fclose(fp);  
  26. }  
  27. main()  
  28. {  
  29. int i;  
  30. for(i=0;i<SIZE;i++)  
  31. {  
  32. scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);  
  33. save();  
  34. }  
  35. for(i=0;i<SIZE;i++)  
  36. {  
  37. printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);  
  38. }  
  39. }  
  40. fread.c  
  41. #include <stdio.h> 
  42. #define SIZE 2  
  43. struct student_type  
  44. {  
  45. char name[10];  
  46. int num;  
  47. int age;  
  48. char addr[10];  
  49. }stud[SIZE];  
  50. void read()  
  51. {  
  52. FILE *fp;  
  53. int i;  
  54. if((fp=fopen("stu_list","rb"))==NULL)  
  55. {  
  56. printf("cant open the file");  
  57. exit(0);  
  58. }  
  59. for(i=0;i<SIZE;i++)  
  60. {  
  61. if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)  
  62. printf("file write error\n");  
  63. }  
  64. fclose(fp);  
  65. }  
  66. main()  
  67. {  
  68. int i;  
  69. read();  
  70. for(i=0;i<SIZE;i++)  
  71. {  
  72. printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);  
  73. printf("\n");  
  74. }  

 

C++文件操作相关方法就为大家介绍到这里。

责任编辑:曹凯 来源: 博客园
相关推荐

2011-06-17 16:09:04

freadfwrite

2010-03-26 10:46:20

Python嵌入CC++

2010-02-03 09:59:42

C++文件流操作

2010-02-05 10:46:10

C++文件流

2011-07-20 17:16:50

C++重载函数

2010-02-01 15:26:44

C++ inline函

2010-02-02 17:47:59

C++操作剪贴板

2010-02-03 15:58:51

C++ timer

2010-03-26 11:00:55

Python嵌入CC++

2010-03-24 10:06:37

Python嵌入C++

2010-01-25 17:55:38

C++头文件

2010-02-03 15:52:55

C++ clock()

2010-02-02 10:07:59

C++全局函数

2010-02-04 14:29:45

C++ typenam

2010-02-06 16:21:35

C++常规DLL

2010-02-06 17:21:20

C++ CreateT

2010-03-26 11:00:55

Python嵌入CC++

2010-02-06 11:19:33

C++获取文件

2010-01-21 14:28:03

C++静态成员函数

2011-07-13 11:34:58

CC++时间函数
点赞
收藏

51CTO技术栈公众号