Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C stdio fseek() Function

❮ C stdio Library


Example

Read the character at position 4 from the start of the file:

FILE *fptr;
fptr = fopen("filename.txt", "r");

fseek(fptr, 4, SEEK_SET);
char c = fgetc(fptr);
printf("%c", c);

fclose(fptr);

Definition and Usage

The fseek() function moves the position indicator to a specified position in the file.

The fseek() function is defined in the <stdio.h> header file.


Syntax

fseek(FILE * fptr, long int offset, int origin);

Parameter Values

Parameter Description
fptr Required. A file pointer, usually created by the fopen() function.
offset Required. Specifies a position in the file relative to the origin.
origin Required. Specifies the position in the file from which the offset is applied. It can be one of the following constants:

SEEK_SET - Offset is relative to the beginning of the file
SEEK_CUR - Offset is relative to the current position in the file
SEEK_END - Offset is relative to the end of the file

The SEEK_END value may not be fully supported by some implementations of the library.

Technical Details

Returns: An int value which is zero if successful and non-zero if an error occurred.

More Examples

Example

Find the size of the file by moving the position indicator to the end of the file and then checking its position

// Open a file
FILE *fptr;
fptr = fopen("filename.txt", "r");

// Move the position indicator to the end of the file
fseek(fptr, 0, SEEK_END);

// Read the position
int position = ftell(fptr);

// Display the value
printf("%d", position);

fclose(fptr);

❮ C stdio Library

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.