---restore content starts---
Read again the knowledge point of the document, intermittent has been seen 2-3 times, it took a little time to do a summary, I would like to go back to the book, haha.
1. Buffer-based file operation
2. Open Close File
3. Single character reading and writing
4. Document Pointer positioning
5. String reading and writing
6, block read block write
7. Format Read/write
one, read and write operations must go through a buffer, meet certain conditions and then read into
and write.
standard input and output, internal buffer-based file operations.
Standard input: stdin
standard output: stdout
standard error: stderr
Cleanup buffer: fflush ();
Second, open the closed file
In the C language, use the file structure to describe the details of the open files. The file definition is inside the stdio.h.
1. Open function
Prototype: FILE *fopen (const char *path, const char *mode); The first parameter is the path the second one is open
Absolute path or relative path: the path to the current project relative to the top-level directory
The project has a 1.txt: "D://_ccccccc/1.txt"--Pair path "1.txt"--relative path
Parameter 2: Open mode--read, write
Return value: Opens a pointer to the file after successful return operation. Failed to return null
The program code is as follows:
1FILE *fp_open (void)2 {3 StaticFILE *FP;4fp = fopen (file_name,"w+");//Open or create a TXT file. 5 if(fp = =NULL)6printf"File opened succed!"); 7 Else8printf"File opened failed!");9 returnFP;Ten } One intMainvoid) A { -FILE *FP = Fp_open ();//defines a file pointer to a file opened in "w+" mode - return 0; the}
Here it is necessary to master the Mode mode table: Details see figure
Open mode |
Meaning |
"R/RB" (Read only) |
Open a text/binary file for input |
"W/WB" (write only) |
Open or create a text binary file for the output |
"A/ab" (Append) |
Append data to the end of a text/binary file |
"R+/rb+" (Read and write) |
Open a text/binary file for read/write |
"W+/wb+" (Read and write) |
Create a text/binary file for read/write |
"A+/ab+" (Read and write) |
Open or create a text/binary file for read/write |
Attention:
A file opened with "R" can only be read, and the file must exist, and cannot open a nonexistent file with "R"
The file opened with "W" can only be written, if it does not exist, open a file that was created with the name of the file, if you open the original file, then from the beginning of the file to resume writing, the original content will be overwritten.
A file opened with "a" can only be written, and the file must exist, at which point the file pointer moves to the end of the file and continues to add new data from the end.
"B" means that a binary file is open, r+ or w+ indicates that read and write operations can be performed.
2. Close function
fclose (file *FP);//Close the file that the FP points to
Three, single character reading and writing
Write: FPUTC prototype: int FPUTC (int c, FILE *stream); Correct return CH, error return EOF (-1)
READ: FGETC prototype: int fgetc (FILE *stream); Correct return of Read CH, error return EOF (-1)
The test function is as follows:
1 intMainvoid) 2 {3 Charch;4FILE *FP = Fp_open ();//Fp_open () function see above5ch = getchar ();//Enter a character from the console6FPUTC (CH,FP);//writing characters to a file7Rewind (FP);//Resets the file pointer to the file header8printf"%c", FGETC (FP));//print out the characters you read9 return 0;Ten}
Note:
PUTC (CH,FP) works the same as FPUTC (CH,FP), but PUTC can be defined as macros, faster
getc (FP) works the same as FGETC (FP), but getc can be replaced as a macro faster.
Putchar (c) is FPUTC (c,stdout);
GetChar () is fgetc (stdin);
Here is a description of the differences between GETC and GetChar () and PUTC and Putchar (), and the relationship between the two functions
input prototype: getc:int __cdecl getc (file *_file); Enter a character into a file
getchar:int __cdecl getchar (void); This is a variant of getc, and the formal parameters of the specified file are stdin;
Output prototype: putc:int __cdecl putc (int _ch,file *_file), output from a certain file
putchar:int __cdecl putchar (int _ch);
Four, file pointer positioning
1, rewind (file *FP);//Position the read and write pointer to the beginning of the file.
The Rewind function uses the reference above function specifically,
2, fseek (file *fp,long offset,int whence);//read/write to the file that the FP points to//parameter 2: means cheap bytes parameter 3: Indicates the reference location
/ * Below the standard library, contains these three macro definitions: Seek_set: Start position seek_end: End Seek_cur: Current position */
Rewind () is actually a special function of Fsee ()
Rewind (FP) = Fseek (Fp,0,seek_set); the two are equal.
3.int feof (file *FP);//Determines whether the file read-write pointer reaches the end, reaches return 1, does not return 0
Similarly, the function is also similar to Fseek ().
v. String reading and Writing
written by: fputs
prototype: int fputs (const char *s,file *stream),//return value, returns the last ' s ' of the input string, and returns the last character entered if the input process is interrupted
READ: Fgets
Prototype: Char *fgets (char *s, int n, FILE *stream); Returns a value that returns the first address of the received string, and returns null if the failure is unsuccessful
The procedure is as follows:
1 intMainvoid) 2 {3 Chars[ -];4FILE *FP = Fp_open ();//defines a file pointer to a file opened in "w+" mode5Gets (s);//Enter a string6Fputs (S,FP);//Enter the contents of the string into my file7 Rewind (FP);8printf"%s\n", Fgets (S,5, FP));//reads a five-byte string from a file (including the last ' + ', so only 4 is actually removed)9 return 0;Ten}
The note here is that the number of reads is a string of n bytes, containing the last ' s ', so at the time of testing, only 4 characters are output, and the last byte is ' + '.
Block Read Block write
written by: fwrite
prototype: size_t fread (void *buffer,size_t size, size_t count, FILE *FP);
read: Fread
prototype: size_t fwrite (void *buffer,size_t size, size_t count, FILE *FP);
Features:
Fread: Reads count blocks from the file pointed to by the FP, each block is of size bytes, and is stored in memory with the first address in buffer (buffer must be space-opened),
Fwrite: Reads count in memory from the first address of buffer, each fast length is a size byte, and is written to the file pointed to by the FP.
return value: 1 Success 0 error or file trailer
vii. format reading and writing
fscanf prototype: int fscanf (FILE *fp,const char *format[,address,?])
fprintf prototype: int fprintf (FILE *fp,const char *format[,argument,?])
In fact, these two functions can be analogous to printf () to print data to the console and scanf () to input data from the console.
fscanf () function is??????????????????????????????
The fprintf () function is the input of data into a file in some format. There is a problem, enter the decimal int number, why follow the string output will be able to output???
Viii. Description of some other functions
1. Ftell () function
prototype: Long Ftell (FILE *fp)
function: Returns the position of the pointer at the current position (represented by the amount of displacement relative to the beginning of the file)
return Value: Success, return the current position pointer position, failed to return -1l;
2. Ferror () function
prototype: int ferror (FILE *fp)
function: Test file for errors
return value: no error, 0; error, not 0
Note: Every time the file input and output function is called, a new Ferror function value is generated, so the ferror function should be automatically set to 0 when fopen Open file is tested in time .
3. Remove () function
function Prototype: int remove (char* path)
function: Delete Files of the specified path
return value: Successfully deleted, 0; otherwise,-1
4. Clearerr () function
function prototype: void Clearerr (FILE *fp)
function: Set the file error flag to 0
return value: None
Description: After an error, the error flag remains until the same file is Clearerr (FP) or rewind or any other input/output function
5. File operation steps
1. Open File
file* fp = fopen ("Path: File name", "Open with");//r/w/rb/wb/a/r+/w+ text and binary files
1>. Read-write single character: FPUTC () fgetc ()
2> read-write String: Fputs () fgets ()
3>. read-write struct, etc. (read-write block) fwrite () fread ()//binary
Span style= "FONT-SIZE:18PX; Font-family: the song body; >4>. Format read-write fprintf () fscanf ()
3. Close file
Summary of File functions
1, FPUTC, fgetc, feof, rewind
2, Rewind, fseek
3, fputs, fgets
4, Fread, fwrite
5, FSCANF, fprintf
6, int ftell (FILE *fp)//Fseek (Fp,0,seek_end), Ftell (FP);//
---restore content ends---
"Three torches"---C file learning