Using the C function fseek

Table of contents:

Using the C function fseek
Using the C function fseek
Anonim

Reading from the file is carried out sequentially, that is, after the next read operation, the pointer will be set to the next unread character. You can change the position of the pointer using the fseek() function. It is used to move the file pointer to a specific position. The fseek() function is defined in the standard C library - stdio.h, so at the beginning of the program in which the function will be used, there must be a declaration: include.

Using the fseek function in C
Using the fseek function in C

Description of the fseek function in C

Int fseek(FILE FilePointer, long offset, int pos) - the following arguments are passed to the function:

  • FILE FilePointer - a pointer to a FILE object. Before using the function, you must open the file using the fopen() function.
  • Offset - this argument passes to the function by how many bytes the pointer should be shifted. It has type long integer (long int). A positive value of the parameter means an offset to the right, and a negative value to the left.
  • Pos - defines the position from which the offset is added. The type of the given argument is an integernumber (int).

The pos parameter defines the starting point from which the offset will be counted. It can take three values - 0, 1, 2, which are equivalent to the symbolic constants SEEK_SET(0), SEEK_CUR(1) and SEEK_END(2):

  1. The value of the pos parameter is SEEK_SET - the offset will be determined relative to the beginning of the file.
  2. When set to SEEK_CUR, the offset is calculated from the current cursor position.
  3. If it is set to SEEK_END, then the offset will be counted from the end of the file.

The C-function fseek returns zero if the pointer was successfully moved, and any nonzero value if it failed to perform the required action, for example, if the pointer went out of bounds. The return value will be EBADF if an invalid file pointer is passed to the function, EINVAL if the argument value is invalid, or ESPIPE if there is an error in the offset parameter, such as out-of-bounds file.

si fseek
si fseek

Example

Program

Description

include

int main()

{

FILE fp;

fp=fopen("test.txt", "r");

fseek(fp, 7, SEEK_CUR);

printf("%ld", ftell(fp));

fclose(fp);

return 0;

}

Include the standard C library stdio.h

Set file variable

Open file for reading

Move the pointer 7 bytes forward from the current position(because we just opened the file, the pointer is at the beginning)

Printing the current cursor position using the ftell() function

Close the file

Besides the above, there are other functions for working with a pointer, for example:

  • Rewind() - used to set the pointer to the beginning of the file.
  • Ftell() - returns the current position of the pointer.
  • Feof() - serves to identify the end of the file. When the end of the file is reached, the value of the function will be true.

Closing remarks

When working with this function, you need to remember that it only works with streaming I / O. Also, don't forget to close the file with the fclose() function.

Popular topic

Editor's choice

  • What is a proxy server and what is it for?
    What is a proxy server and what is it for?

    The word "proxy" has ever been heard by any of us, but not everyone knows what a proxy server is and what its purpose is

  • Google Chrome not working. What to do?
    Google Chrome not working. What to do?

    So why doesn't Chrome launch when we click on its desktop icon? Google Chrome not working for several reasons

  • Recovery console. Main benefits of using
    Recovery console. Main benefits of using

    Computers are quite technically complex devices. In this regard, many users from time to time encounter failures in the operation of the operating system and other programs. In most cases, you can fix problems in a matter of minutes, especially if the computer technician has the necessary skills. If a critical failure occurs, it is recommended to use the recovery console

  • What is the cluster size?
    What is the cluster size?

    The space of any storage medium (hard drive or flash drive) is not a whole piece, but a system of memory cells called clusters

  • What is defragmentation and why is it needed?
    What is defragmentation and why is it needed?

    Modern users are often spoiled by powerful computers and inexpensive components to such an extent that they do not even know the basic concepts. That is why they often find themselves in a situation where the car begins to shamelessly “slow down” and respond with extreme reluctance to any commands. As a rule, “evil viruses” that have entered the computer are blamed for everything, but sometimes the reality turns out to be much more prosaic