C Programming

How to Use Lseek System Call in C

The Lseek System call has been used to read some specific characters or data from a file or to write some content at the specific location of any file. This means you can read or write from in between the content of a file.

Syntax:

Off_t lseek(int fd, off_t offset, int whence);

This “lseek” system call requires two header files, e.g., “sys/types.h” and “unistd.h”. The lseek system call requires three parameters. The first one is “fd,” which is a file descriptor. The second is “offset,” used to position the pointer. And the third parameter, “whence,” is used to specify the position of a file pointer, e.g., beginning, end, mid.

Install Manpages:

To check the extra information regarding system calls, you have to install the manpages-dev package. So try to launch the Linux shell terminal using the shortcut key Ctrl+Alt+ T or search it from the activity area. After you have successfully launched the command terminal, execute the below apt install query to install the manpages-dev library. It requires your current account passcode to proceed with the installation. Enter the password and tap on the Enter key from the typewriter. The process of installation will be started and start collecting information.

$ sudo apt install manpages-dev

The installation process will make you wait for a little time. After the installation got complete, you can now check the information regarding the “lseek” command.  So, write the below “man 2” command along with the name of the system call. As you can see, we have been checking for “lseek” in the below instruction.

$ man 2 lseek

The man page for the “lseek” system call has been opened below. You can get all the information regarding it.

Install GCC Compiler:

Now it’s time for us to install the GCC compiler for the compiling of C language files. So, you have to use a very simple apt install command in the shell to install it while using the compiler extension as keyword name below.

$ sudo apt install gcc

It will take your little time to get its installation done. After installing the gcc package, your Linux system is ready to use c language files in it.

Example 01:

Before going deep to understand the concept of the lseek system call, you have to start it with a simple type of file. So, launch the terminal and try the below touch command to create a simple file named “seek” in the home directory of your Linux distribution.

$ touch seek

Add some data into the file “seek” and save it. You can see the file “seek” contents in the command-line shell by using the simple “cat” query below. The content of a file has some alphabets and numbers along with the sign “*”.

$ cat seek

Let’s create a C type file “test.c” using the nano keyword to directly open it with the Nano editor. Try to execute the below command to do so.

$ nano test.c

Let’s check one program without the lseek command. It will open a nano text editor with a C-type file mentioned on the upper side. Now write the below-shown code from the image in the editor C file. The first 4 lines contain the header files necessary to implement the “lseek” command. After that, the main function has been started. In this main method, we have defined integer and character type data for further use. The third line of the main function has been using the open call to open a file named “seek,” which we have just created above, and the offset “O_RDWR”  has been used for reading and write permissions. The content from the file has been returned to file descriptor “f”. After this, we have used the first read method call to read 12 character data from the seek file. The write function has been used to write data into the screen. Another read command has been used to get the next 12 characters from the file descriptor “f”, and the write command is reading the next 12 characters to the output screen. From this, we can assume that the output will show a total of 24 characters from the file “seek”. Press Ctrl+S to save this file and try Ctrl+X to close it.

Let’s just compile the “test.c” file by using the GCC compiler command below.

$ gcc test.c

Now run the code of the “test.c” file to check the output of the C code as given below. The output has displayed the 24 character set of content from the file seek as below.

$ ./a.out

Let’s use the “lseek” command in the code to check the output of a system call. So open the same test.c file using the below nano command.

$ nano test.c

Update your code as it is. This time we have been using 6 characters from the content file to be read and write. The lseek command has been used to skip the next 5 characters from the content file and jump to the next characters using the “SEEK_CUR” offset.

Let’s compile this code again.

$ gcc test.c

The output of the C code using the a.out command shows the first 6 characters, then it skips the next 5 characters and then shows the consecutive next 6 characters.

$ ./a.out

Example 02:

To have some change, let’s change the code. Open the file first.

$ nano test.c

Now we will be using the “SEEK_SET” offset to tell the “lseek” system call to show the next 6 characters from the 10th position of a string.

Compile the C file.

$ gcc test.c

The output command a.out has been showing the 6 characters from the 10th index of a string.

Conclusion:

This guide has done some simple examples of using the “lseek” system call command to check or show content or data from any position we want.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.