Within this guide today, we will be using the “Read” 2 c function call to read data from a file. Let’s get started with the article now. Firstly, we have been moving within the directory “src” using the “cd” query with the file name. While using the list “ls” instruction in the “src” directory of Kali Linux, we have got to know that it contains only a single file within it, i.e., “lkn_example.”
Let’s create a new C file within the “src” folder where we are currently working in. For this, you can try to use the “touch” instruction to create one and open the particular file within the Nano editor using the “nano” instruction with the name of a file in the Kali Linux shell. Other than that, you can try out the “vim” instruction. The “vim” instruction can let you create and automatically open the C file within the “Vim” editor. We have been naming it “readFile” with the “c” extension at its end, as displayed in the attached image below.
Our newly created C file “readFile.c” has been automatically opened in the Vim editor as an empty file. Now, we can start making C code within it. Write out the shown-below code in your file for execution. Let’s take a look at this code now. This code has been started with the main C standard libraries, which are required to smoothly run this code at our end. After this, we have been covering this example with the simple main() method of the C language. We have initialized two variables, fileDescriptor, and size, to be used in the further code. The calloc() function is used to assign memory of 100 characters to the character pointer “c.” The file descriptor variable has been making use of the open() function to open the “SomeFile.txt” from the current working directory with the read-only option, i.e., “O_RDONLY.”
If the file descriptor value goes less than 0, i.e., file not opened, then it will display an error message using the “perror()” method and exit the program. Otherwise, the size variable will be getting the size of bytes to be read by the fileDescriptor using the “read” function, and the printf function would be displaying the fileDescriptor along with the size to be used. The “c” variable will take that size and get characters from the file according to the size, and the printf function will be displaying those.
It’s time to compile our C code within Kali Linux with the GCC compiler. Make sure to have GCC installed and configured at your end. You need to try out the gcc command along with the name of a C file, i.e., readFile.c followed by the –o option and the name of an execution file to be generated from this compilation process. The execution file must contain the “.out” extension at its end, i.e., we have been naming the execution file as “readFile.out”.
After the successful compilation, we have now got the compiled and error-free code within the readFile.out file in the “Src” folder. As you can see that the “ls” list instruction has been showing that the main source code file and execution file after compilation is both listed here.
To run the error-free compiled file “readFile.out”, try out the file name with the “./” command as shown below. The output for this command displayed the error that there is no such file or directory in the current working directory “src,” which you have been looking for.
To avoid this error, we must have the “SomeFile.txt” named file in the “src” working directory. So, we have been creating the same name file within the “src” folder using the “vim” instruction. The list “ls” command shows that the file has been generated perfectly.
We have added the displayed below two lines in the “SomeFile.txt” file.
Now, when you run the C code file again with the “./” character, it will execute the print statement showing that the total of 10 bytes would be read out from the file. After that, it displayed the total of the first 10 characters from the “SomeFile.txt” file, i.e., “Hello, Enj.”
Let’s open the same C file “readFile.c” with the “vim” command once again to update it.
After opening it, we have updated the number of bytes to be read from the file, i.e., replaced 10 with 10 in the “read” system call. Saved this code to avoid any inconvenience.
After compiling this file again with the “gcc” compiler and running its error-free execution file “readFile.out” on the Kali Linux shell, we got to know that both the lines from the “SomeFile.txt” file of the “Src” folder have been read out, i.e., contains less than 100 characters.
Conclusion
This article is all about the use of the read 2 functions of C programming to read data in bytes from any sort of file. We have discussed the use and benefit of this function within the introductory paragraph, and for better understanding, we have added a detailed illustration of the C program.