C Programming
In this tutorial we will learn to randomly access file data in C programming language.
In this tutorial we will be discussing the fseek(), ftell() and rewind() functions to access data in a file.
fseek()
ftell()
rewind()
The ftell() function tells us about the current position in the file (in bytes).
Syntax:
pos = ftell(fptr);
Where, fptr is a file pointer. pos holds the current position i.e., total bytes read (or written).
fptr
pos
Example:
If a file has 10 bytes of data and if the ftell() function returns 4 then, it means that 4 bytes has already been read (or written).
We use the rewind() function to return back to the starting point in the file.
rewind(fptr);
Where, fptr is a file pointer.
We use the fseek() function to move the file position to a desired location.
fseek(fptr, offset, position);
Where, fptr is the file pointer. offset which is of type long, specifies the number of positions (in bytes) to move in the file from the location specified by the position.
offset
long
position
The position can take the following values.
Following are the list of operations we can perform using the fseek() function.
fseek(fptr, 0, 0)
fseek(fptr, 0, 2)
fseek(fptr, N, 0)
fseek(fptr, N, 1)
fseek(fptr, -N, 1)
fseek(fptr, -N, 2)
#include <stdio.h> int main(void) { // integer variable int i; // character variable char ch; // file pointer FILE *fptr; // open file in write mode fptr = fopen("char", "w"); if (fptr != NULL) { printf("File created successfully!\n"); } else { printf("Failed to create the file.\n"); // exit status for OS that an error occured return -1; } // write data in file for (ch = 'A'; ch <= 'Z'; ch++) { putc(ch, fptr); } // close connection fclose(fptr); // reference printf("\nReference:\n"); for (i = 0; i < 26; i++) { printf("%d ", (i+1)); } printf("\n"); for (i = 65; i <= 90; i++) { // print character printf("%c", (i)); // manage space if (i - 65 >= 9) { printf(" "); } else { printf(" "); } } printf("\n\n"); // open file for reading fptr = fopen("char", "r"); printf("Curr pos: %ld\n", ftell(fptr)); // read 1st char in the file fseek(fptr, 0, 0); ch = getc(fptr); printf("1st char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // read 5th char in the file fseek(fptr, 4, 0); ch = getc(fptr); printf("5th char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // read 26th char in the file fseek(fptr, 25, 0); ch = getc(fptr); printf("26th char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // rewind printf("rewind\n"); rewind(fptr); printf("Curr pos: %ld\n", ftell(fptr)); // read 10th char in the file fseek(fptr, 9, 0); ch = getc(fptr); printf("10th char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // read 15th char in the file fseek(fptr, 4, 1); // move 4 bytes forward from current position ch = getc(fptr); printf("15th char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // read 20th char in the file fseek(fptr, 4, 1); // move 4 bytes forward from current position ch = getc(fptr); printf("20th char: %c\n", ch); printf("Curr pos: %ld\n", ftell(fptr)); // close connection fclose(fptr); return 0; }
Output:
File created successfully! Reference: 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 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Curr pos: 0 1st char: A Curr pos: 1 5th char: E Curr pos: 5 26th char: Z Curr pos: 26 rewind Curr pos: 0 10th char: J Curr pos: 10 15th char: O Curr pos: 15 20th char: T Curr pos: 20