fseek() in C Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The fseek() function is part of the C standard library that is used to move the file pointer associated with a given file to a specific location in the file. It provides random access capabilities, which allow the programmer to randomly move the file pointer to any location in the file to read or write data.Syntax of fseek()The fseek() function is declared in the <stdio.h> header file. C int fseek(FILE *pointer, long int offset, int position); Parameterspointer: It is the pointer to a FILE object that identifies the stream.offset: It is the number of bytes to offset from the positionposition: It is the position from where the offset is added. Position defines the point with respect to which the file pointer needs to be moved. It has three values:SEEK_END: It denotes the end of the file.SEEK_SET: It denotes starting of the file.SEEK_CUR: It denotes the file pointer's current position. Return ValueIt returns zero if successful, or else it returns a non-zero value.Examples of fseek()The following programs demonstrate the use of fseek() function in our C programs:Move File Pointer to End of File C #include <stdio.h> int main() { FILE* fp; fp = fopen("test.txt", "r"); // Moving pointer to end fseek(fp, 0, SEEK_END); // Printing position of pointer printf("%ld", ftell(fp)); return 0; } Suppose the file test.txt contains the following data:"Someone over there is calling you. we are going for work. take care of yourself."Output81ExplanationWhen we implement fseek(), we move the pointer by 0 distance with respect to the end of the file i.e. pointer now points to the end of the file. Therefore the output is 81.Move File Pointer to Start of File C #include <stdio.h> int main() { FILE* fp; fp = fopen("test.txt", "r"); // Moving pointer to start fseek(fp, 0, SEEK_SET); // Printing position of pointer printf("%ld", ftell(fp)); return 0; } Suppose the file test.txt contains the following data:"Someone over there is calling you. we are going for work. take care of yourself."Output0ExplanationIn the above program we use fseek() to move the pointer by 0 distance with respect to the start of the file i.e. the pointer now points to the start of the file. Thus the current position fo the pointer is 0, Therefore the output is 0. Comment More infoAdvertise with us Next Article fwrite() in C K kartik Improve Article Tags : C++ CPP-Library C-File Handling C-Library Practice Tags : CPP Similar Reads fgets() in C In C, fgets() is a built-in function defined in <stdio.h> header file. It reads the given number of characters of a line from the input stream and stores it into the specified string. This function stops reading the characters when it encounters a newline character, has read the given number o 3 min read fseek() vs rewind() in C In C, the functions fseek() and rewind() helps in manipulating the position of the pointer in the opened file but serve different purposes and have distinct working. The below table lists the major differences between fseek() and rewind() in C:Featurefseek()rewind()FunctionalityMoves the file pointe 2 min read fwrite() in C In C, fwrite() is a built-in function used to write a block of data from the program into a file. It can write arrays, structs, or other data to files but is especially designed to write binary data in binary files.Example:C#include <stdio.h> int main() { FILE *fptr = fopen("gfg.bin", "wb"); i 3 min read fprintf() in C fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt 1 min read C fread() Function The C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by f 4 min read C fopen() Function In C, the fopen() function is used to open a file in the specified mode. The function returns a file pointer (FILE *) which is used to perform further operations on the file, such as reading from or writing to it. If the file exists then the fopen() function opens the particular file else a new file 5 min read Like