Syntax
It has the following syntax:
long ftell(FILE *stream);
Parameters
- stream: This parameter denotes a pointer to the FILE object associated with the file stream.
- Return Value: Upon successful execution, it furnishes a long integer that signifies the present position within the file. If an error occurs, it will return -1L.
Example code using ftell in C
Let's consider an example to demonstrate the application of the ftell function in the C programming language.
Example
#include <stdio.h>
int main() { //main function
FILE *file;
long position;
// Open file in write mode and write some text
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
fputs("Hello, world!", file); // Write to file
position = ftell(file); // Get current position
printf("Current file position after writing: %ld\n", position);
fclose(file);
// Open file in read mode and read few characters
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
char ch;
for (int i = 0; i < 5; i++) {
ch = fgetc(file); // Read characters
printf("%c", ch);
}
position = ftell(file); // Get position after reading 5 characters
printf("\nFile position after reading 5 characters: %ld\n", position);
fclose(file);
return 0;
}
Output:
Current file position after writing: 13
Hello
File position after reading 5 characters: 5
Explanation:
In this illustration, we showcase the utilization of the ftell function to monitor the current position in a file pointer. Initially, a file is opened in write mode, a string is written to it, and then the position is determined using the ftell function. Following the closure and subsequent reopening of the file in read mode, a reading operation of 5 characters is performed, and once more the ftell function is employed to exhibit the updated pointer position.
Example to use the ftell function with fseek function in C
Let's consider an example to illustrate the utilization of the ftell function in conjunction with the fseek function in the C programming language.
Example
#include <stdio.h>
int main() { //main function
FILE *file;
long pos;
// Open the file in write mode
file = fopen("sample.txt", "w+");
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
// Write a string to the file
fputs("ABCDE12345", file);
// Move file pointer to the beginning
fseek(file, 0, SEEK_SET);
printf("Pointer reset to beginning.\n");
// Read and display first 3 characters
for (int i = 0; i < 3; i++) {
printf("%c", fgetc(file));
}
// Get and print current file position
pos = ftell(file);
printf("\nCurrent file pointer position: %ld\n", pos);
// Move file pointer 2 characters ahead from current position
fseek(file, 2, SEEK_CUR);
pos = ftell(file);
printf("File pointer moved 2 bytes ahead, now at: %ld\n", pos);
// Move file pointer to 5 bytes from end
fseek(file, -5, SEEK_END);
pos = ftell(file);
printf("File pointer 5 bytes from end, now at: %ld\n", pos);
fclose(file);
return 0;
}
Output:
Pointer reset to beginning.
ABC
Current file pointer position: 3
File pointer moved 2 bytes ahead, now at: 5
File pointer 5 bytes from end, now at: 5
Explanation:
In this instance, we illustrate the utilization of fseek and ftell to manage and monitor the location of a file pointer in the C programming language. Initially, we write information to a file, adjust the pointer using various offset references (beginning, current, end), and exhibit its position following each action. This technique is beneficial for implementing random access in file manipulation.
Conclusion
In summary, the ftell function in C proves to be a valuable tool for controlling file operations. Providing the precise byte position of the file pointer, it simplifies tasks like bookmarking, file navigation, and aligning read and write positions. When used alongside functions like fseek and rewind, ftell enables adaptable and effective file handling, be it for binary or text files.
Ftell Function in C FAQ's
1) What is the purpose of C's ftell function?
The ftell function returns the current position of the file pointer in bytes starting from the beginning of the file.
2) What is returned by the ftell function?
The long integer representing the byte offset from the start is provided, and if there is an error, -1L is returned.
Yes, can the ftell function be utilized with both text and binary files?
Yes, the ftell function is applicable for both binary and text files. Due to potential encoding differences or newline conversions, the output position in text files may not always accurately reflect the byte position.
The distinction between fseek and ftell lies in their functions within file handling operations. While fseek is used to move the file position indicator to a specified location in a file, ftell serves to determine the current position of the file position indicator within the file.
In the realm of C programming, the ftell function provides the current position, while fseek positions the file pointer to a designated location.
When we utilize ftell with an open file in append mode, the function will return the current position indicator at the end of the file. This means that it will provide the offset from the beginning of the file to the end, as appending does not change the current position indicator.
The response is that regardless of whether the file was opened in append mode ("a" or "a+"), ftell will consistently provide the position at the end of the file before any writing takes place.