int main(int argc, char * argv[]){ FILE *stream; char buf[20]; if ((stream = fopen("/Users/dtecadmin/Desktop/File.c", "rw+")) == NULL) { fprintf(stderr,"Cannot open output file.\n"); return 1; } fseek(stream,-6,2); fread(buf, 1,sizeof(buf),stream); printf("%s\n", buf); fclose(stream); return 0;}
Fopen
Function Introduction
Function: open a file
Function prototype: file * fopen (const char * path, const char * mode );
Required database: <stdio. h>
Returned value: After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
In general, after opening a file, some operations will be performed to read or write the file. If the file fails to be opened, the subsequent read/write operations will not proceed smoothly () then, make error judgment and handling.
Parameter description:
The parameter path string contains the path and file name of the file to be opened. The parameter mode string represents the stream format.
Mode has the following forms of strings:
R: open the file in read-only mode. The file must exist.
R + can open a file in read/write mode. The file must exist.
RB + read/write opens a binary file and allows reading data.
RW + read/write opens a text file, allowing reading and writing.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
WB only opens or creates a new binary file; only data can be written.
WB + enables read/write operations or creates a binary file, allowing read and write operations.
Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
Fseek
Function to relocate the internal position pointer of a file on a stream (data stream/file)
Note: The file pointer does not point to the file/stream. The position Pointer Points to the byte location inside the file. As the file reads, the file pointer will not change to another file if it is not assigned a value.
Int fseek (File * stream, long offset, int fromwhere );
The description function sets the position of the file pointer stream. If the execution is successful, stream points to the position of fromwhere (starting position of the Offset: File Header 0, current position 1, end 2) as the benchmark, and offset (pointer offset) bytes. If the execution fails (for example, the offset value exceeds the file size), the position pointed to by the stream is not changed.
If the return value is successful, 0 is returned. If the return value is failed,-1 is returned.
Fread
Introduction
Function prototype: size_t fread (void * buffer, size_t size, size_t count, file * stream );
Function: read data from a file stream and read count elements, each element size byte. If the call is successful, return count. If the call is successful, the actual size * count bytes are read.
Parameters:
Buffer
The memory address used to receive data. The size must be at least size * count bytes.
Size
Size of a single element, in bytes
Count
Number of elements, each of which is a size byte.
Stream
Input stream
Returned value: Number of actually read elements. If the returned value is different from Count (not count * size), the end of the file may be incorrect.
Function Name: rewind
Function: points the position pointer inside the file to the beginning of a stream.
Int main (INT argc, char * argv []) {file * stream; char * MSG = "Haha "; if (Stream = fopen ("/users/dtecadmin/desktop/file. C "," W + ") = NULL) {fprintf (stderr," cannot open output file. \ n "); return 1;} fwrite (MSG, strlen (MSG), 1, stream); fclose (Stream); Return 0 ;}
File opening method:
"RT"
Read-only opens a text file and only allows reading data
"WT"
Only write to open or create a text file, only data can be written
""
Append a text file and write data at the end of the file.
"RB"
Read-only open a binary file and only allow reading data
"WB"
Only write to open or create a binary file, only data can be written
"AB"
Append a binary file and write data at the end of the file.
"RT +"
Read and Write
"WT +"
Read/write to open or create a text file, allowing read/write
"At +"
Open a text file, allow reading, or append data at the end of the file.
"RB +"
Open a binary file for read and write operations.
"WB +"
Read/write to open or create a binary file, allowing read and write
"AB +"
Open a binary file, allow reading, or append data at the end of the file.