C fread() Function (original) (raw)

Last Updated : 17 Sep, 2024

The **C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by fread() function is the number of elements read multiplied by the size of each element in bytes.

Syntax of C fread()

size_t **fread(void * _buffer, size_t _size, size_t _count, FILE * _stream);

The file position indicator is automatically moved forward by the number of bytes read. If the objects being read are not trivially copy-able, such as structures or complex data types then it does not behave properly.

Parameters

Return Value

**Note: fread() function itself does not provide a way to distinguish between end-of-file and error, feof and ferror can be used to determine which occurred.

Examples of C fread()

Example 1

The below programs illustrate the fread() function.

C++ `

// C program to use the fread() function to read the content of binary file into an array. #include <stdio.h>

int main() { FILE *file; int buffer[5];

// Open the binary file for reading
file = fopen("input.bin", "rb");
if (file == NULL) {
    perror("Error opening file");
    return 1;
}

// Read the integers from the file into the buffer
fread(buffer, sizeof(int),5, file);

// Print the integers that were read
for (int i = 0; i < 5; i++) {
    printf("Element %d: %d\n", i + 1, buffer[i]);
}

// Close the file
fclose(file);
return 0;

}

`

**Output

Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50

Here, **input.bin file should contain the binary representation of the integers: 10, 20, 30, 40, 50.

Example 2

The below programs demonstrates the use of the fread() function to read data from a file and store it in a buffer.

C `

// C program to illustrate fread() function #include <stdio.h> int main(){ // File pointer FILE *filePointer;

// Buffer to store the read data
char buffer[100];
size_t bytesRead;

// "Gfg.txt" file is opened in read mode
filePointer = fopen("Gfg.txt", "r");

// Ensure the file was opened successfully
if (!filePointer){
    printf("Error opening file.\n");
    return 1;
}

// Data is read from the file into the buffer
while ((bytesRead = fread(buffer, 1, sizeof(buffer) - 1, filePointer)) > 0){
  
  // Null-terminate the buffer
    buffer[bytesRead] = '\0'; 
  
    // Print the read data
    printf("%s", buffer);
}
fclose(filePointer);
return 0;

}

`

Suppose the file Gfg.txt contains the following data:

Geeks : DS-ALgo
Gfg : DP
Contribute : writearticle

Then, after running the program, the output will be

Geeks : DS-ALgo
Gfg : DP
Contribute : writearticle

Example 3

This C program demonstrates the usage of the fread() function when the file's size or count is equal to 0.

C `

// C program to illustrate fread() function // when size of the file or the value of count is equal to 0

#include <stdio.h>

int main() { // File pointer FILE* filePointer; // Buffer to store the read data char buffer[100]; // "g4g.txt" file is opened in read mode

filePointer = fopen("g4g.txt", "r");
// Case when count is equal to 0
printf("count = 0, return value = %zu\n",
       fread(buffer, sizeof(buffer), 0, filePointer));
// Case when size is equal to 0
printf("size = 0, return value = %zu\n",
       fread(buffer, 0, 1, filePointer));
return 0;

}

`

Output

count = 0, return value = 0 size = 0, return value = 0