If you want to know more on ftell in c then read this blog post.
What is ftell in c
ftell in c is used to get total size of a file after moving file pointer to the end of a file.
It returns the current file pointer position from starting of a file.
It is declared in stdio.h header file.
It works along with fopen and fclose functions on a file.
Prototype of ftell in c programming
long int ftell(FILE *strm);
Parameters for ftell function in c
It is pointed to a current file pointer position in a stream.
Return value of ftell function in c
It returns current file pinter position w.r.t. starting of a file.
Example program for ftell in c programming
#include <stdio.h>
int main () {
FILE *fpointer;
int length;
fpointer = fopen("scholar.txt", "r");
if(fpointer == NULL) {
printf("file error");
return(-1);
}
fseek(fpointer, 0, SEEK_END);
length = ftell(fpointer);
fclose(fpointer);
printf("Size of file: %d bytes", length);
return 0;
}
Code explanation
The file pointer is initialized to point the given file.
The total size of a given file is calculated by calling ftell function and passing the file pointer to it. The result is being printed onto the console.
Difference between fseek and ftell in c
fseek is used to change the file pointer to the specified stream. ftell in c is used to get the total size of a file after moving the file pointer to the end of a file.
The fseek function returns zero in operation is successful otherwise a nonzero value. ftell returns the current file pointer position from the starting of a file.
fseek takes three arguments. ftell takes one argument.
Both work on a position of a file pointer. If there is a need to read it in bytes then use fread function.
Prototypes are as follows
int fseek(FILE *strm,long int offst,int whnce);
long int ftell(FILE *strm);
fseek in c | ftell in c |
Used to change the file pointer to the specified stream. | Used to get total size of a file after moving file pointer to the end of a file. |
int fseek(FILE *strm,long int offst,int whnce); | long int ftell(FILE *strm); |
Returns zero in operation is successful otherwise a nonzero value. | Returns the current file pointer position from starting of a file. |
Takes three arguments. | Takes one argument. |
Difference between fwrite and ftell
fwrite is used to write binary data to a file. ftell in c is used to get the total size of a file after moving the file pointer to the end of a file.
fwrite returns the total number of items written in a file. ftell returns the current file pointer position from the starting of a file.
fwrite takes four arguments. ftell takes one argument.
fwrite takes two pointers where one pointer points to an array of elements to be written. ftell takes one argument.
Prototypes are as follows
size_t fwrite(const void *pt,size_t size,size_t nmeb,FILE *strm);
long int ftell(FILE *strm);
ftell in c | fwrite in c |
Used to get total size of a file after moving file pointer to the end of a file. | Used to write binary data to a file. |
long int ftell(FILE *strm); | size_t fwrite(const void *pt,size_t size,size_t nmeb,FILE *strm); |
Returns the current file pointer position from starting of a file. | Returns total number of items written in a file. |
Takes one argument. | Takes four arguments. |
Difference between fread and ftell in c
ftell in c is used to get the total size of a file after moving the file pointer to the end of a file. fread is used to read data from a file.
ftell returns the current file pointer position from the starting of a file. fread returns an integer value on successful operation otherwise EOF is returned.
ftell takes one argument. fread takes four arguments.
ftell does not uses buffer. fread uses buffer.
Prototypes are as follows
size_t fread(void *pointer,size_t size,size_t nmeb,FILE *strm);
long int ftell(FILE *strm);
ftell in c | fread in c |
Used to get total size of a file after moving file pointer to the end of a file. | Used to read a data from a file. |
long int ftell(FILE *strm); | size_t fread(void *pointer,size_t size,size_t nmeb,FILE *strm); |
Returns the current file pointer position from starting of a file. | Returns an integer value on successful operation otherwise EOF is returned. |
Takes one argument. | Takes four arguments. |
Difference between fscanf and ftell in c
fscanf() function is used to read the data from file. ftell in c is used to get the total size of a file after moving the file pointer to the end of a file.
fscanf() function returns the number of characters that it converted and assigned otherwise EOF if an error occurs. ftell returns the current file pointer position from the starting of a file.
fscanf takes many arguments. ftell takes one argument.
fscanf removes all whitespaces while reading. ftell does not remove whitespaces.
Prototypes are as follows
int fscanf(FILE *strm,const char *frmt,...);
long int ftell(FILE *strm);
ftell in c | fscanf in c |
Used to get total size of a file after moving file pointer to the end of a file. | Used to read the data from file. |
long int ftell(FILE *strm); | int fscanf(FILE *strm,const char *frmt,...); |
Returns the current file pointer position from starting of a file. | Returns the number of characters that it converted and assigned otherwise EOF if error occurs. |
Takes one argument. | Takes many arguments. |
Difference between fprintf and ftell in c
fprintf function in c is used to format the output to a file instead of stdout console. ftell in c is used to get the total size of a file after moving the file pointer to the end of a file.
fprintf returns the number of characters written. if any error occurs it returns a negative value(-1). ftell returns the current file pointer position from the starting of a file.
fprintf takes many arguments. ftell takes one argument.
ftell works better in binary mode than in text mode. fprintf works both in binary and text mode.
Prototypes are as follows
int fprintf(FILE *file_pointer, const char *format_string,.......);
long int ftell(FILE *strm);
ftell in c | fprintf in c |
Used to get total size of a file after moving file pointer to the end of a file. | Used to format the output to a file instead of stdout console. |
long int ftell(FILE *strm); | int fprintf(FILE *file_pointer, const char *format_string,.......); |
Returns the current file pointer position from starting of a file. | Returns the number of characters written.if any error occurs it returns negative value(-1). |
Takes one argument. | Takes many arguments. |
People also ask
What is the difference between fputs and ftell in c?
fputs is used to write array of characters to a file. ftell is used to take total size of a file.
What is the difference between fgetc and ftell in c?
fgetc is used to get single character in a file. ftell is used to take total size of a file.
What is the difference between feof and ftell in c?
feof is used to get end of a file indicator in a file. ftell is used to take total size of a file.
What does ftell stands for in c?
Return a file offset in a file. It returns total size of a file.
What are all the errors if ftell fails?
Following are the errors if ftell fails
1.EBADF
2.EOVERFLOW
3.ESPIPE
What is the difference between fseek and ftell in c?
fseek is used to move a pointer to a specific position in a file. ftell is used to get the total size of a file.
People also read
- File handling in c
- feof in c
- fopen in c
- fclose in c
- getw in c
- putw in c
- fseek in c
- fscanf in c
- fprintf in c
- File input output in c