| by Arround The Web | No comments

Read SysCall in Linux

“As the title says that the read system call must be reading something from a particular location. Linux provides us the read system call to read data from any file and display it on the console, and the kernel of the Linux system should be involved in this whole process, i.e., to open the file, read from it, write data to another file, etc. This guide will be a bonus to every Linux and C user who wants to learn to use the read system call while coding. Before having a glance at the C code snippet, we are going to create a text file and a C file. The text file will contain random text data that will be used by the read() function. Both the files are created by the same “touch” query one after another, and the “ls” query shows them in a home folder.”

We have added 3 lines of text in the textfile.txt file so that we can access it using the read() function of C. Those 3 lines are displayed using the “cat” query.

Example 01

In our first example, we will be demonstrating the use of the read() system call or POSIX function to access the data from a specific file and display it on the console. The example code starts with the basic 3 header files. The stdio.h and unistd.h headers are used for input/output standard mutable functions. The fcntl.h is used to utilize the POSIX read and write functions for creating and accessing the files. The snippet shown below has been initiated with the declaration of a character buffer of size 100. The text data that has been shown in the textfile.txt file from the above image will be accessed and displayed in chunks.

The printf() statement is utilized 3 times to let us know about the number of total characters going to be fetched from the file and displayed on the console. Three file descriptors, f1, f2, and f3, are used separately to call the “open()” function of C to open a textfile.txt file in a read-only mode using the “O_RDONLY” option. The variables n1, n2, and n3 are utilized to fetch the returned result from calling the “Read” function 3 times, respectively.

Each time, we have been passing it a character type buffer or array, i.e., Arr, a different file descriptor, i.e., f1, f2, f3, and a different number of characters to be accessed from the file and saved to buffer, i.e., 20, 40, and 60 respectively. Each time, we have been using the returned result, i.e., n1, n2, n3, within the write() function call to access the buffer variable “Arr” with the first parameter as “1”. This write function would display 20, 40, and 60 characters after reading from the file textfile.txt into the character buffer “Arr”. The printf() statement has been used each time to add a line break using “\n” in its string area.

In this way, every time, some text data would be displayed on the shell after the read() function access it from the file “textfile.txt.” Let’s quickly save our code before the compilation.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
  char Arr[100];
  printf("Reading 20 characters..........\n");
  int f1 = open("textfile.txt", O_RDONLY);
  int n1 = read(f1, Arr, 20);
  write(1, Arr, n1);
  printf("\n");
  printf("Reading 40 characters..........\n");
  int f2 = open("textfile.txt", O_RDONLY);
  int n2 = read(f2, Arr, 40);
  write(1, Arr, n2);
  printf("\n");
  printf("Reading 60 characters..........\n");
  int f3 = open("textfile.txt", O_RDONLY);
  int n3 = read(f3, Arr, 60);
  write(1, Arr, n3);
  printf("\n");
}

After compilation of this code with the “GCC” C compiler, we have tried the “./a.out” command to run the compiled code. It shows 20, 40, and 60 characters from the text file using “read.”

Example 02

Let’s say you have shown-below data in a textfile.txt file, and you want to transfer an exact number of characters to another file. For this, you will be utilizing the read() function to read the data from the source file, and the write function will be used to save it to the destination file.

Now, we are going to use the same headers that we utilized in the above example to read from a file to a buffer variable. The main() method of this code is initialized with a size 40-character array “Arr.” The printf() output statements are utilized at each step to explain each and everything in detail, i.e., the opening of a file, reading from a file, creating another file, copying data from the first file to the second file, etc. Along with that, we have been using the sleep() function at the initialization of each step in this example code. After the first sleep() function, we have been using the open() function to open our file named “textfile.txt” in a read mode and save the return result to file descriptor fd1.

In the next step, we are going to use the read() function to read the data from the textfile.txt using its file descriptor fd1. You have to note one thing; we are only reading the 30 characters from the file and saving those to the character array “Arr.” If the read() function works fine, it will return some success number to the integer variable “n.” In the third step, we have been creating another file named “dest.txt” using the open() function once again.

Two options have been provided to the open function in this step. In the last step, we are casting off the write() system call by passing it the fd2 file descriptor, variable “n,” and buffer array “Arr” to copy the data from the buffer “Arr” to the second file using f2.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
  char Arr[40];
  printf("Opening textfile.txt in read-only mode...\n");
  sleep(3);
  int fd1 = open("textfile.txt", O_RDONLY);
  printf("Reading from it....\n");
  sleep(3);
  int n = read(fd1, Arr, 30);
  printf("Creating another file for writing in it...\n");
  sleep(3);
  int fd2 = open("dest.txt", O_CREAT | O_WRONLY, 0642);
  printf("Writing textfile.txt data to dest.txt file...\n");
  sleep(3);
  write(fd2, Arr, n);
  printf("\n");
}

On this code execution, the first file got opened, and the “Arr” array read the data from it.

The new file was created in a write mode.

The data from the character array “Arr” is passed to the second file.

The 30-character data has been successfully copied to the dest.txt file, as displayed below.

Conclusion

In this guide, we have briefly explained the use of the read() system call in C language. For this, we have utilized the buffer arrays to read data from a specific file and display it on the console. Also, we have used the read() function and buffer array to copy text data from one file to another.

Share Button

Source: linuxhint.com

Leave a Reply