C Programming

pread C function

In C Programming Language, the purpose of the pread() function is to read the specified number of bytes (n) of input which are indicated by the file descriptor, into the defined buffer area of the memory. The pread() function does the same thing as the read() method, but it starts reading from a specific place in the file rather than updating the whole file with the file pointer. The file pointer position is not affected. However, the offset value is utilized to specify the file’s starting location. We must examine how this function operates to see this.

Before we go further, we need to understand the syntax of the pread function and its header files required for this illustration. The unistd header file is used to work with the pread function. So, here’s the code to put in the C Programming Language file:

#include <unistd.h>

The pread() function contains the same initial three parameters as the read() function, and the fourth parameter is offset for the specified position inside the file. Any attempt to run the pread() function on a file that can’t complete a seek action will result in an error. Here is the syntax of the pread function:

ssize_t pread(int File_Descriptor, void *buffer, size_t No_of_N_Bytes, off_t offset);

This function returns a non-negative integer, indicating that it successfully read the specified number of bytes returned by this function. If it returns a negative value, it fails to read the specified number of bytes. It set the errno to EBADF (which means that the file descriptor is not a valid file or socket descriptor), ECONNRESET (a peer forced the termination of a connection), EFAULT, ENOBUFS (insufficient system resources to finish the call), etc.

To get the details of errno, please do some research online. The read function does not require authorization to read a file from the file system or the network.

Example 1

The example below opens a file and reads data from it. To begin this tutorial, start a terminal in Linux, UNIX, or any other operating system you want. Then, as shown below, move from the current directory to another appropriate working directory, or a “works” folder in our case. Use the “ls” command to display almost all files and directories in the working directory. In the VIM editor, open or create a new file titled “pRead.c”, a C programming language file.

We’re using the VIM editor to open and create it. However, you can use whichever editor you like, such as nano, text, etc. You can see that we have a new.txt file in this list of directories. This file will be utilized in our C code now. Look at the following terminal screen to visualize the output:

The header files are included in the first three lines. After that, the main method starts with its first line declaring two integer variables, i.e., num and fileDescriptor. Following that, we set the offset to 5. We define a character array “buffer” and set the value to “Test text” in the third line of the main function. We’re providing the user read-only access to the new.txt file in the conditional expression with the help of an open() function.

The open function is here to return a descriptor value that is recorded in the fileDescriptor variable. If the descriptor gets the value less than 0, the perror() function will be executed to throw an error. In the else statement, we use the pread function to transmit the values of the variables and the sizeof function to decrement the buffer size if the descriptor gets the value greater than 0. The print statement displayed the output.

The following is the content of the new.txt file. The buffer text is on the third line, which is the offset reading point. Look at the following terminal screen to visualize the output:

Now, use the C compiler to compile the pRead.c file. We use the GCC compiler for the compilation and execution of C code. In the terminal, type “gcc” and input the name of the needed C file, followed by the –o switch and the name of the output file seen in the following screenshot. The compiler will display any errors during compilation in the terminal window. The required output file is created if the compilation is successful and may be found in the working directory.

The output may fluctuate depending on the offset value. Similarly, if the value of the buffer is altered, the output on the terminal screen will undoubtedly change. Now, type the following command in the command prompt to run the output file. Look at the following terminal screen to visualize the output:

Now, using the VIM editor, we want to update the pRead file and need to set the value for offset to 8, and for buffer update, the string to “Animals” instead of “Test text” as the code shown in the following image:

Because we made changes to our program code file, now we need to recompile our code. The compilation process is the same as performed in the previous steps. Here is the image of the new compilation screen:

Now, we must print the output on the screen using the output or object file we obtained after completing the compilation procedure. See the output on the terminal screen by typing the following command. The output of this is “Animal”. Look at the following attached terminal screen to visualize the output:

Conclusion

This article showed the use of the pread function of C to read the data from some text files of our Linux system in bytes. We have discussed how a pread() function is different from the simple read() function due to using a specific point to read the data from a file.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.