Files are used to persist data
Persistent: Data does not disappear after power failure. Next time you add, you can read
Properties of the file:
Filename
Path
Length (measured in bytes)
Content
Read-only/read-write
fopen Open a file
Fclose closing files
Fwrite writing data
Fread closing files
Steps to save data to a file
1) fopen Open File
FILE *fopen (const char * filename, const char * mode);
Mode uses WB, writes binary
2) fwrite Write data
size_t fwrite (const void * buf, size_t size, size_t count, FILE * stream);
BUF the data to write
Size is always 1
Count length
Stream fopen the created pointer
3) fclose Close file
Simple example of writing data
#include <stdio.h>intMain () {//Define file name Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } CharBuf[] ="Hello"; //Write DataFwrite (BUF,1,5, FP); intA =0x12345678; Fwrite (&a,1,4, FP); Fclose (FP); return 0;}
Write Data in detail
I. Writing data to an array
The code is as follows
#include <stdio.h>#include<string.h>intMain () {//Define file name Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } intbuf[4] = {0xa001,0xb002,0xc003,0xd004};
for(inti =0; I <4; i++) { Chartext[ -]; //convert int type data to character typesprintf (Text,"%d,", Buf[i]); Fwrite (text,1, strlen (text), FP); } fclose (FP); return 0;}
Two. Specify format to save decimals
#include <stdio.h>#include<string.h>intMain () {//Define file name Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } DoubleA =3/4.0; Chartext[ -]; //keep two decimal placessprintf (Text,"%.2LF", a); Fwrite (text,1, strlen (text), FP); Fclose (FP); return 0;}
Three. Writing a string
Two ways to store strings
char buf [+] = "Hello";
Method 1: Store by actual effective length (using the Strlen function inside the string.h to get the string specific length)
Fwrite (buf, 1, strlen (BUF), FP);
Mode 2: Write by fixed length
Fwrite (BUF, 1, (+), FP);
(More space occupied)
#include <stdio.h>#include<string.h>intMain () {//Define file name Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } Charbuf[ -] ="Hello"; //fwrite (Buf,1,strlen (BUF), FP);
Fwrite (buf,1,
return 0 ;
}
Four. Writing to a struct
#include <stdio.h>#include<string.h>structstudent{intID; Chargender; Charname[ -];};intMain () {//Define file name Const Char* filename ="C:/aaa.txt"; //Open the file, the system will create a file based on the specified path and file nameFILE * fp = fopen (filename,"WB"); //Determines whether the file pointer is empty to determine whether the file was created successfully if(fp = =NULL) {printf ("failed to open file!\n"); return-1; } Student someone= {61031,'M',"Noname"}; //fwrite (&someone, 1, sizeof (someone), FP);fwrite (&someone.gender,1,1, FP); Fwrite (&someone.id,1,4, FP); Fwrite (&someone.gender,1, -, FP); Fclose (FP); return 0;}
fopen (filename, "WB");
When the parameter is WB, the content will overwrite
When the parameter is AB, the written content is appended to the back
C, c + + file operations-file concepts and file writing