Prev Next
fseek(), SEEK_SET, SEEK_CUR, SEEK_END functions in C:
fseek() functions is file handling functions in C programming language. It has following constants. SEEK_SET, SEEK_CUR, SEEK_END. Please find below the description and syntax for each above file handling functions.
File operation
|
Declaration & Description
|
fseek() | Declaration: int fseek(FILE *fp, long int offset, int whence)
fseek() function is used to move file pointer position to the given location. where, |
SEEK_SET | SEEK_SET – It moves file pointer position to the beginning of the file. |
SEEK_CUR | SEEK_CUR – It moves file pointer position to given location. |
SEEK_END | SEEK_END – It moves file pointer position to the end of file. |
Example program for fseek(), seek_set(), seek_cur(), seek_end() functions in C:
Assume that test.c file is loaded with following data. “Fresh2refresh.com is a programming tutorial website”. Let’s see how to move file pointer to different locations inside a file in below C program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> int main () { FILE *fp; char data[60]; fp = fopen ("test.c","w"); fputs("Fresh2refresh.com is a programming tutorial website", fp); fgets ( data, 60, fp ); printf(Before fseek - %s", data); // To set file pointet to 21th byte/character in the file fseek(fp, 21, SEEK_SET); fflush(data); fgets ( data, 60, fp ); printf("After SEEK_SET to 21 - %s", data); // To find backward 10 bytes from current position fseek(fp, -10, SEEK_CUR); fflush(data); fgets ( data, 60, fp ); printf("After SEEK_CUR to -10 - %s", data); // To find 7th byte before the end of file fseek(fp, -7, SEEK_END); fflush(data); fgets ( data, 60, fp ); printf("After SEEK_END to -7 - %s", data); // To set file pointer to the beginning of the file fseek(fp, 0, SEEK_SET); // We can use rewind(fp); also fclose(fp); return 0; } |
Output:
Before fseek – Fresh2refresh.com is a programming tutorial website
After SEEK_SET to 21 – a programming tutorial website After SEEK_CUR to -10 – sh.com is a programming tutorial website After SEEK_END to -7 – website |
Other Inbuilt file handling functions in C programming language:
C programming language offers many other inbuilt functions for handling files. They are given below. Please click on each function name below to know more details, example programs, output for the respective file handling function.
File handling functions
|
Description
|
fopen () | fopen () function creates a new file or opens an existing file. |
fclose () | fclose () function closes an opened file. |
getw () | getw () function reads an integer from file. |
putw () | putw () functions writes an integer to file. |
fgetc () | fgetc () function reads a character from file. |
fputc () | fputc () functions write a character to file. |
gets () | gets () function reads line from keyboard. |
puts () | puts () function writes line to o/p screen. |
fgets () | fgets () function reads string from a file, one line at a time. |
fputs () | fputs () function writes string to a file. |
feof () | feof () function finds end of file. |
fgetchar () | fgetchar () function reads a character from keyboard. |
fprintf () | fprintf () function writes formatted data to a file. |
fscanf () | fscanf () function reads formatted data from a file. |
fputchar () | fputchar () function writes a character onto the output screen from keyboard input. |
fseek () | fseek () function moves file pointer position to given location. |
SEEK_SET | SEEK_SET moves file pointer position to the beginning of the file. |
SEEK_CUR | SEEK_CUR moves file pointer position to given location. |
SEEK_END | SEEK_END moves file pointer position to the end of file. |
ftell () | ftell () function gives current position of file pointer. |
rewind () | rewind () function moves file pointer position to the beginning of the file. |
getc () | getc () function reads character from file. |
getch () | getch () function reads character from keyboard. |
getche () | getche () function reads character from keyboard and echoes to o/p screen. |
getchar () | getchar () function reads character from keyboard. |
putc () | putc () function writes a character to file. |
putchar () | putchar () function writes a character to screen. |
printf () | printf () function writes formatted data to screen. |
sprinf () | sprinf () function writes formatted output to string. |
scanf () | scanf () function reads formatted data from keyboard. |
sscanf () | sscanf () function Reads formatted input from a string. |
remove () | remove () function deletes a file. |
fflush () | fflush () function flushes a file. |