C Programming

Ftell Function in C

Like many other programming languages, the C language came up with many in-built functions. These functions have been added within the C language to perform different functions at different levels.

One of the functions is the “ftell()” function of the C filing concept. This function is most probably used to read the existing file location of the provided stream relative to the file’s beginning. Upon shifting the document pointer to the last location of the document, this method is being rummage-sale to retrieve the overall size of the dataset. The present location is returned in long format, and the document can contain over 32767 bytes of information. Thus, we have decided to cover the C ftell() in Ubuntu 20.04 system.

Start with Ubuntu’s terminal launch, i.e., Ctrl+Alt+T. We have to make a new text file in the home folder of our Ubuntu system. Make use of touch instruction to do so. Go towards the home folder of your Linux system and open the newly created file by double-tapping on it. Add some text data in it, save and close it.

You can see we have created the file.txt file and added the below-shown data in it. We will be utilizing this file in our near-to-come examples.

$ cat file.txt

Example 01:

Let’s make up with our first examples for this article. You need to create a new C-type file in your system using Ubuntu’s touch instruction along with the name of a file using the “.c” extension. This file is needed to open within one of Ubuntu’s editors to use it for a code. You can use the vim, text, or nano editor. We have been using the “GNU Nano” editor so far to make our code more appealing.

$ touch ftell.c

$ nano ftell.c

When the file got opened in the GNU nano editor, it will be initially empty. We have to start our C code with the use of headers, most probably “stdio.h”. We will be performing our code within the main() function of this code.

Start the main() function of the “int” return type. We have declared the pointer type file descriptor using the FILE object. Another variable of integer type is also declared, i.e., “length”. Now, we have to open the already existing text file from our system using the fopen() function of the C file to read its data with the reading writes, i.e., “r”. If the file got successfully opened, the file descriptor would be returned with some integer value, most probably “1”.

We have been utilizing the “if” statement to check if the file descriptor value we have just got is Null or not. If so, it will call the perror() method of C to generate an error prompt, i.e., “There is some Error”. After this “if” statement, if the file is successfully opened, we will be using the seek() function of C filing to take the pointer to the end of a file.

We have to utilize the SEEK_END option as the third argument of the fseek() function. The 0 in this function is used to look for the pointer from the 0 positions. The length of the total file size has been obtained from the use of the ftell() function.

The file has been closed with the fclose() function utilizing the file descriptor in it. Now, the printf statement is here to display the total bytes of data within our text file using the “length” variable. Save your code file with the Ctrl+S shortcut.

#include <stdio.h>

void main () {
   FILE *fd;
   int length;
fd = fopen("file.txt", "r");
if(fd == NULL) {
perror(“There is some Error”);
return(-1);
   }
fseek(fd, 0, SEEK_END);
   length = ftell(fd);
fclose(fd);
printf("Our File Size: %d bytes\n", len);
return(0);
}

Exit the nano editor with Ctrl+X shortcut compile the C code via the “gcc” compiler. Run your code after that with the “./a.out” command. You can see that our text file has a total of 370 bytes.

$ gcc ftell.c

$ ./a.out

Example 02:

Let’s take a new example to use the ftell() function of C within it. Use the same code file to make our code updated. Use the “stdio.h” header and start the main() function. Use the point type file descriptor “f” to open the text file “file.txt” in the read mode. Declare an array of size 10, i.e., “A”. We have been using the fscanf() function of C filing to scan the first string from the text file having no more than 10 letters.

The printf() statement has been using the file descriptor in its ftell() function to display the length of the first string from its text file as well.

#include <stdio.h>

void main () {
   FILE *f = fopen("file.txt", "r");
   char A[10];
fscanf(f, "%s", A);
printf("Position of Pointer: %ld\n", ftell(f);
return(0);
}

After this code compilation and running, this file shows us the output, i.e., the position of the pointer after the 1st string is 4, as shown below.

$ gcc ftell.c

$ ./a.out

Let’s make the first string in the file.txt file a little longer using simple editing.

$ cat file.txt

Now, use the same amount of code with no update/change in it to see how it results. We have been using the array of size 10 for the “A” array. If the size is lesser, it will display the total size of the first string, i.e., length but show some output on the output area as well.

The execution of this code shows us that the size is 28, but the program is smashed because the first string is of 28 sizes, and you have been using an area of size 10.

$ gcc ftell.c

$ ./a.out

Conclusion:

This article discussed the minor details of using the ftell() function of C to get the size of data used within some particular document. We have discussed two of the simple illustrations to explain the concept of ftell function in the C programming language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content