Introduction to the specific application functions of C ++ File Operations

Source: Internet
Author: User

The C ++ programming language is flexible and can be seen as an upgraded version of the C language. We can use the methods related to C ++ File Operations introduced in this article to have a preliminary understanding of the application skills related to this computer programming language, and deepen the cognition of this language.

  • Summary of C ++'s learning thoughts
  • Tips for compiling Visual C ++ applications
  • Introduction to C ++ function pointers
  • Summary of C ++ TinyXML usage
  • Two types of C ++ Data Pointer

1. Functions in C ++ File Operations

Used to read and write a data block.

2. General call form

Fread (buffer, size, count, fp );

Fwrite (buffer, size, count, fp );

3. Description

1) buffer: A pointer. For fread, It is the storage address for reading data. For fwrite, It is the address of the data to be output.

2) size: the number of bytes to read and write;

3) count: the number of size-byte data items to be read and written;

4) fp: file pointer.

Note: After completing the fwrite () operation, you must close the stream (fclose ());

2. After a C ++ FILE operation (fread () is completed, if the stream is not closed (fclose (), the pointer (FILE * fp) the system automatically moves the length of the previous read/write operation backward. If the stream is not closed, the next read operation is continued, and the last output is continued;

3 fprintf (): Input to the stream in the format. Its prototype is int fprintf (FILE * stream, const char * format [, argument,...]); the method is the same as printf (), but it is not written to the console, but to the stream. Note that the returned value is the number of bytes written to the file for this operation. For example, int c = fprintf (fp, "% s % d % f", str1, str2, a, B); str1: 10 bytes; str2: 10 bytes;: 2 bytes; B: 8 bytes; c: 33, because a space is automatically added between different data during writing.

You must disable the file after it is used. Otherwise, the content cannot be correctly displayed. fwrite: Read the information of two students and store the information in the file using fwrite.

Fread: Use fread to read student information from the file.

 
 
  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 ++ file operations are described here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.