| by Arround The Web | No comments

Fseek() in C

As a daily computer user, we are always working with files and folders. These files and folders are the must-haves in any operating system like Windows or Linux. Apart from accessing the files through the operating system, we can also access these files and folders within any programming language through filing. Though it’s not as simple as using the files and folders in operating systems, it can provide us with a sufficient amount of information.

The “fseek” function of “C” is designed to get an information regarding the specific range of text data within any type of file like the total number of characters, display the specific text data, and many more. As we have been working on the Linux platform, it’s necessary to install the C compiler through the “command-line” application. Therefore, quickly open your “terminal” application and try updating your Ubuntu 22.04 Linux system first. Use the “apt” utility with “sudo” rights in the “update” query to update it.

After the whole system is updated, we are going to configure the “C” compiler in our Linux machine. It’s a need for our code examples because, without a compiler, we will be unable to execute the codes. Thus, utilizing the “apt” tool once again, we install the “GCC” compiler for the “C” language using the “install” query shown in the following image:

Let’s create a text file before creating a C code. This text file is produced using the Linux “touch” query as shown in the following illustration. After this, we add some text in the new file. Five (5) lines of text data are displayed in the shell as well via the “cat” instruction.

Example 01:

We start this first illustration of C to elaborate on how we can use the fseek function to move the cursor to the specific point of the file and count the total number of characters within a specific range. Thus, the “touch” query is used again to create a file with the “c” extension, i.e. “fseek.c”. The list query shows this newly produced file within the home folder.

To start coding within the C file, we open this file in the GNU Nano Editor of the Linux system. Now, the C file must start with the “stdio.h” header because it’s a must for the utilization of the input and output statements of C. After including the standard header, we use the “main” function. We use the built-in function within the main() function, so there is no need to add any user-defined function.

The “FILE” object is used to create a pointer type file descriptor “f”. This file descriptor helps us to access and modify the file wherever possible. We use the “fopen” function of C to simply open the “fseek.txt” file in a read mode using the “r” argument. The fopen() function returns some value to the descriptor “f” that tells us whether the file is successfully launched or not. The “f” file descriptor helps us perform the seeking on a text file via the “fseek” function by placing the pointer at the “0” index until the end of the file, i.e. “SEEK_END” option.

The “C” language “printf” statement makes use of the “ftell” function with the “f” file descriptor to tell us the total number of characters in a file from index 0 to the end.

#include <stdio.h>

int main() {

  FILE *f;

  f = fopen("fseek.txt", "r");

  fseek(f, 0, SEEK_END);

  printf("Total number of characters: %ld \n", ftell(f));

  return 0;

  }

We save our code and compile it using the “gcc” compiler instruction on the shell. Upon running this compiled code, we get “69” as the output of the total characters in a file.

Example 02:

In this C program example, we will see how we can add the text data to a file using the specific location via the fseek() function. Therefore, we utilize a little same code with a lot of modifications in it. Starting from adding the input and output and adding the main() method, we initialize a pointer file descriptor using the “FILE” object. This descriptor takes its value as the returned value of the “fopen()” function which is trying to open a file in a write mode.

The “fputs” function of C is used here to add some data into a file. And the fseek() function sets the location of a cursor to 0 index at the beginning. Again, using the fputs() function, we add a text at the start of a file via the “f” descriptor. The fclose() function uses the “f” file descriptor to close the text file after writing into it. We save this code before running it.

#include <stdio.h>

int main() {

  FILE *f;

  f = fopen("fseek.txt", "w+");

  fputs("Middle End!", f);
 
  fseek(f, 0, SEEK_SET);

  fputs("Start", f);

  fclose(f);

  return 0;

}

Upon compilation, we found no errors and executed it with the “./a.out” Linux execution command. The execution did not return anything but made some changes to the “fseek.txt” file. The cat instruction shows that the text “Start” is added to the starting position of a “Middle End” text.

After this, we update the code once again and update the position of a pointer to be placed in the file, i.e. position index 6. The remaining is not bothered by a single word change.

#include <stdio.h>

int main() {

  FILE *f;

  f = fopen("fseek.txt", "w+");

  fputs("Middle End!", f);

  fseek(f, 6, SEEK_SET);

  fputs("Start", f);

  fclose(f);

  return 0;

}

When we compiled this newly updated code with the “gcc” compiler along with running it via the “./a.out” query, it did not return anything as it has made some changes to the text file. Upon displaying the data of the “fseek.txt” file, it shows that the word “Start” is added at the end of a file after the specified position index “6”.

Conclusion

This guide is a bonus to the C users to understand the usage of the fseek() function of C more accurately with the simple examples. For this, we tried to make the Linux system up to date and configured the C compiler. Along with that, we employed the example of C to use the fseek() function for placing the cursor at specific points in the file. We also tried the different functions like the ftell() function to display the total characters from the file and fputs() function to add the data to a file.

Share Button

Source: linuxhint.com

Leave a Reply