catalogue
Zero foundation C/C + + learning route recommendation: C/C + + Learning directory >> Introduction to basic C language
1, Introduction to fseek function
fseek function Used to move the read-write position of the file stream; Just like the player, you can directly drag it to a wonderful point in time. The fseek function is declared as follows
#include <stdio.h> #include <stdlib.h> /* *Description: file read / write offset * *Parameters: * [in] stream: File pointer handle * [in] offset: Offset, a positive number indicates a positive offset (backward offset), and a negative number indicates a negative offset (forward offset) * [in] fromwhere: Set where to start the offset from the file. The possible values are: SEEK_CUR, SEEK_END or SEEK_SET * *Return value: if the execution is successful, the function returns 0. If the execution fails, the function returns a non-zero value. If the execution fails (for example, offset exceeds the size of the file itself), the position pointed to by the stream will not be changed. */ int fseek(FILE *stream, long offset, int fromwhere);
Note: the parameter from where is one of the following:
-
SEEK_SET offset from the beginning of the file is the new read / write position
-
SEEK_CUR will increase offset by one displacement from the current read / write position
-
SEEK_END points the read / write position to the end of the file, and then adds offset displacement
fseek(fp,100L,SEEK_SET); // Move the stream pointer to 100 bytes from the beginning of the file;
fseek(fp,100L,SEEK_CUR); // Move the stream pointer to 100 bytes from the current position of the file;
fseek(fp,-100L,SEEK_END); // Return the stream pointer to 100 bytes from the end of the file.
2, fseek function practice
/******************************************************************************************/ //@Author: ape programming //@Blog (personal blog address): www.codersrc.com //@File:C language tutorial - C language file reading and writing fseek function //@Time:2021/07/24 07:30 //@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly! /******************************************************************************************/ #include <stdio.h> #Include < stdlib. H > / / fseek function call int main() { // The content in the start file is 0123456789 FILE * fp = fopen("a.txt", "r+"); if (fp == NULL) { printf("file error\n"); exit(1); } fseek(fp, 2, SEEK_SET);//Move the cursor to the second byte from the beginning of the file. fwrite("yun", 1, 3, fp); //Write content in file fclose(fp); return 0; } /* Original file content: 0123456789 File contents after running the program: 01yun56789 */
If the execution fails (for example, the offset exceeds the size of the file itself), the position pointed to by the stream will not be changed;
fseek function Pointer to a file that should be an open file. If there is no open file, an error will occur;
The fseek function can also be understood in this way, which is equivalent to locating in a file. In this way, when reading the regular storage file, you can use its OFFSET to read any content on the file;
3, Guess you like it
- Difference between C language array subscript out of bounds and memory overflow
- C language uses pointers to traverse arrays
- Difference between C language pointer and array
- Difference between C language pointer array and array pointer
- C language field pointer
- C language function value transfer and address transfer
- C language function indefinite length parameter
- C language function pointer
- C language pointer function
- C language callback function callback
- C language #pragma once
- Difference between C language #include < > and #include ""
- C language const modifier function parameters
- Difference between const and define in C language
- C language # operators
- C language ## operators
- C language__ VA_ARGS__
- C language##__ VA_ARGS__
- C language function indefinite length parameter##__ VA_ARGS__ Classic case
- C language va_start / va_end / va_arg custom printf function
- C language main function
- C language main function parameter main(int argc, char *argv [])
- C language local variables
- C language global variables
- Differences between global variables and local variables in C language
- C language static
- C language extern
No reprint without permission: Ape programming ยป C language file reading and writing fseek function