Read Line by Line Linux System Call
A system phone call is a method for software to communicate with the operating system. When software performs a system call, it sends the request to the kernel of the operating system. To read by a file descriptor, you tin use the read () organization function. Each process has its personal file descriptors table in the operating system. The sole difference betwixt read () and write () is that read () reads data from the file referred to past the file descriptor. The reading time for the file is updated after a successful read ().
Syntax
# ssize_t read(int fd, const void *buf, size_t count);
The file descriptor is the kickoff argument. The buffer is the second argument. Finally, the third option specifies how many bytes you consider reading. Bytes is just the volume of data to copy, and the buffer is the address of the memory space where data is to be written out. The render upshot represents the quantity of data written in bytes; if information technology varies from bytes, something seems to be incorrect. If the value is negative, the organization telephone call volition abort.
Pre-requisites
The GCC compiler is necessary to compile the code that contains a read() system call. To execute and assemble C language code, we'll need to install a compiler package on our motorcar. So, nosotros'll need to install the GCC compiler too. You can install it with the presented below command in a Linux terminal, as shown below. It will install in just a few moments, and you lot volition be able to simply write and run C codes in files.
By striking the enter fundamental, the GCC compiler volition be installed. Information technology will take a few moments before asking you to say aye or no. If you choose aye, it will begin screening the output of the GCC compiler. Employ the below-appended teaching to install GCC on your arrangement.
Offset Case of Read() System Call in C
Let's get-go with read () to get a amend understanding of information technology. First, yous must utilise the trounce's GNU editor to generate a C-type file. Try running the simple query below in the last for this.
It will take a bit of time for the to a higher place-mentioned command to open up, merely once it does, yous will be able to apply it immediately. It launches your Ubuntu twenty.04'southward GNU editor right away. The GNU editor volition appear as shown in the image fastened beneath. At present, write the displayed code in the editor.
The read () system call receives the user's input from the keyboard that is the file descriptor as 0 and puts it in the buffer "buff", which is but a character assortment. It can simply read up to x bytes at a time. Merely the kickoff x letters will be read, regardless of how much the user inputs. Lastly, using the write () system call, the data is displayed on the window.
On the display, it prints the very same x bytes as from the buffer from the file descriptor 1. You should now compile the code shown above. We need to use the GCC compiler for the code compilation. To compile the "read1.c" file, run the GCC control beneath. Return to the console and write the command:
Now, we must run this code using the./a.out command in the terminal every bit seen beneath.
As per the program, the output is displayed in the in a higher place-fastened screenshot.
Second Case of Read () System Call in C
Y'all can't predict how much the user would enter equally input. As a result, the tertiary statement to the write () organisation cannot be used to provide the right bytes. The result may differ from what you lot expected. Keep in mind what read () returns if it succeeds. To demonstrate this concept, we are executing this example. At present over again use the shell's GNU editor to generate a C-type file. Attempt running the simple query below in the concluding for this.
Information technology will take a scrap of time for the higher up-mentioned command to open, merely once information technology does, yous will be able to apply it immediately. It launches your Ubuntu 20.04'south GNU editor right abroad. The GNU editor will appear every bit shown in the image fastened below. At present, write the displayed code in the editor.
In this code, we utilise the nread variable to hold the number of bytes that are read by the read () arrangement telephone call in C, and utilize the variable in write () to display the same amount of bytes on the window. You should at present compile the code shown above.
We demand to use the GCC compiler for the lawmaking compilation. To compile the "read.c" file, run the GCC control beneath. Return to the panel and type the command:
Once the code is compiled, run this code using the ./a.out control in the final, as seen below.
Every bit per the plan, the output is displayed in the above-attached screenshot.
Conclusion
To comprehend the concept of the Read () organization call in Ubuntu, we have explored a few simple and linear examples in C. Nosotros've also covered how to set up the GCC compiler for code compilation. I hope you can at present simply use the Read () system call to build your code on Ubuntu 20.04.
Source: https://linuxhint.com/read-system-call-in-c/
0 Response to "Read Line by Line Linux System Call"
Enregistrer un commentaire