C language file reading and writing fseek function - C language zero basic introductory tutorial

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

  1. Difference between C language array subscript out of bounds and memory overflow
  2. C language uses pointers to traverse arrays
  3. Difference between C language pointer and array
  4. Difference between C language pointer array and array pointer
  5. C language field pointer
  6. C language function value transfer and address transfer
  7. C language function indefinite length parameter
  8. C language function pointer
  9. C language pointer function
  10. C language callback function callback
  11. C language #pragma once
  12. Difference between C language #include < > and #include ""
  13. C language const modifier function parameters
  14. Difference between const and define in C language
  15. C language # operators
  16. C language ## operators
  17. C language__ VA_ARGS__
  18. C language##__ VA_ARGS__
  19. C language function indefinite length parameter##__ VA_ARGS__ Classic case
  20. C language va_start / va_end / va_arg custom printf function
  21. C language main function
  22. C language main function parameter main(int argc, char *argv [])
  23. C language local variables
  24. C language global variables
  25. Differences between global variables and local variables in C language
  26. C language static
  27. C language extern

No reprint without permission: Ape programming ยป C language file reading and writing fseek function

Keywords: C

Added by bills50000 on Tue, 12 Oct 2021 06:29:26 +0300