Using the c function fseek

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 specified in the standard C library - stdio.h, therefore, at the beginning of the program in which the function will be used, a declaration must be present: #include <stdio.h>.

Using fseek function in si




Description of fseek function in si

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 - through this argument, the function passes by how many bytes the pointer should be offset. It has a type of long integer (long int). A positive parameter value means an offset to the right, and a negative one to the left.
  • Pos - defines the position from which the offset is added. The type of this argument is an integer (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 pos is SEEK_SET - the offset will be determined relative to the beginning of the file.
  2. If set to SEEK_CUR, the offset is calculated from the current cursor position.
  3. If it has the value 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 non-zero value if it could not perform the required action, for example, if the pointer went beyond the file boundaries. The returned value will be equal to EBADF when the file pointer is passed to the function, EINVAL if the argument value is invalid, or ESPIPE if the offset parameter fails, for example, out of file limits.

si fseek




Example

Program

Description

#include <stdio.h>



int main()



{



FILE *fp;



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



fseek(fp, 7, SEEK_CUR);



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



fclose (fp);

return 0;



}



We connect the standard language library With stdio.h

Set the file variable

Open the file for reading

We move the pointer 7 bytes forward from the current position (since we just opened the file, the pointer is at the beginning)

We print the current cursor position using the ftell () function

Close the file

In addition to the above, there are other functions for working with the 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 function value will be true.

Concluding observations

When working with this function, you need to remember that it only works with streaming input-output. Also, do not forget to close the file with the fclose () function.




All Articles