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.
- fwrite.c
- #include <stdio.h>
- #define SIZE 2
- struct student_type
- {
- char name[10];
- int num;
- int age;
- char addr[10];
- }stud[SIZE];
- void save()
- {
- FILE *fp;
- int i;
- if((fp=fopen("stu_list","wb"))==NULL)
- {
- printf("cant open the file");
- exit(0);
- }
- for(i=0;i<SIZE;i++)
- {
- if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
- printf("file write error\n");
- }
- fclose(fp);
- }
- main()
- {
- int i;
- for(i=0;i<SIZE;i++)
- {
- scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
- save();
- }
- for(i=0;i<SIZE;i++)
- {
- printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
- }
- }
- fread.c
- #include <stdio.h>
- #define SIZE 2
- struct student_type
- {
- char name[10];
- int num;
- int age;
- char addr[10];
- }stud[SIZE];
- void read()
- {
- FILE *fp;
- int i;
- if((fp=fopen("stu_list","rb"))==NULL)
- {
- printf("cant open the file");
- exit(0);
- }
- for(i=0;i<SIZE;i++)
- {
- if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)
- printf("file write error\n");
- }
- fclose(fp);
- }
- main()
- {
- int i;
- read();
- for(i=0;i<SIZE;i++)
- {
- printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
- printf("\n");
- }
- }
C ++ file operations are described here.