C Programming

How to Use fread() Function of C Language

The fread() is a C programming function that allows users to read a specific amount of data from a given file and stores the data information in a buffer or array. The file must be saved in your system on which you are performing C programming. The fread() function is part of the standard C library and is found in the <stdio.h> header file.

This article discusses the fread() function in C programming language and the way to use it in the program.

fread() Function in C Language

The fread() function requires three arguments: the pointer to an array, the size of each element from the array pointer, and the number of elements to read from the stream. It can also take additional arguments for controlling how much data is read, how the data is read, and so on. For example, the syntax for it could be something like this:

fread(array_buffer, element_size, element_count, stream );

The first argument given is the pointer to the array buffer which will be populated with the read data. The array’s second argument is the size of every element in bytes. The third parameter specifies how many elements will be taken from the stream. Let’s implement the following code in which the fread() function is used.

#include <stdio.h>

int main ()

{

    char buffer[33];

    FILE * stream;

    stream = fopen("C_File.txt", "r");

    int count = fread(&buffer, sizeof(char), 33, stream);

fclose(stream);

printf("Data read from the file: %s \n", buffer);

printf("Number of elements read: %d", count);

    return 0;

}

We opened a file from the PC in the above code. The fread() function receives a pointer referring to the FILE object buffer. We supply size as sizeof(char) since we are reading characters from the file stream, and the integer number 33 is passed to the function to indicate that we want to read 33 characters from the input file stream.

Output

The total number of items read is what the fread() function returns. This can be used to ensure that the function is successful. If the return value is less than the number of elements specified, then some elements were not read from the stream.

Benefits of fread() Function

The benefits of a fread() function are:

1: Readability from a Stream

The ability to read data from a stream is one of the fread() function’s most important advantages. This means that you don’t need to manually open and close a file before and after reading, but the data is still correctly read from a stream of bytes. This feature makes the fread() function particularly efficient when dealing with large data sets in which the data must be read in exactly the same order it was written.

2: Safety

In terms of safety, functions in the C standard library also have an associated error code. In the case of fread(), an error code of 0 indicates that the required number of items (in bytes) was not read. This code can be used to handle errors related to corrupt or incomplete data sets.

3: Able to Read Partial Data

The fread() function can also be used to read partial data objects by specifying a number of bytes that is less than the size of the data object. This is useful for complex data types such as structs, where the user may only need to access certain fields of the struct.

4: Versatility

Also, the fread() method has the advantage of being quite flexible. It is highly compatible with any input device and can also be used to read from strings and arrays.

Conclusion

The fread() function is important for programs that need to access and manipulate binary data. When paired with other library functions, it is extremely helpful. The function is relatively simple to use and can be tailored to a program’s specific needs with the addition of optional arguments.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.