fread function-read from file with specified number of bytes

You can use the fread function to read from a file by specifying the number of bytes. You can use it by reading the stdio.h header.

#include <stdio.h>
size_t fread (void * buffer, size_t size, size_t n, FILE * fp);

The first argument is a buffer that stores the contents read from the file. Secure a memory size that can store the read contents. The second argument is the unit of bytes of data to read. The third argument is the number of data to read. The actual byte size to be read is "size * n". The fourth argument is the file stream of the file you want to read.

The return value is the number of data read. It corresponds to the number of data specified by the third argument. Please note that it is not a byte size. When the read reaches the end of the file, the return value will be less than the number of data specified by the third argument.

Sample read by fread function

This is a sample read by the fread function. Open the file with fopen function, read 16 bytes at a time, output to standard output with putchar function, fclose function.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main (void) {
  
  // Open the file in read mode
  const char * in_file = "input.txt";
  FILE * in_fp = fopen (in_file, "r");
  if (in_fp == NULL) {
    fprintf (stderr, "Can't open file%s\n", in_file);
    exit (1);
  }
  
  // Read and output to standard output
  while (1) {
    
    char buffer [16];
    int32_t read_unit = 1;
    int32_t read_count = 16;
    size_t real_read_count = fread (buffer, read_unit, read_count, in_fp);
    
    // Output the read characters
    for (int32_t i = 0; i <real_read_count; i ++) {
      putchar (buffer [i]);
    }
    
    // Line breaks for clarity
    printf("\ n");
    
    if (real_read_count <read_count) {
      break;
    }
  }
  
  // Close the file with the fclose function
  fclose (in_fp);
}

It is an input file "input.txt".

aaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccddddddddddddddddddddd

This is the output result.

aaaaaaaaaaaaaaaa
aaaaaabbbbbbbbbbbb
bbbbbbbbbbbbbbcc
cccccccccccccccc
ccccccccdddddddd
ddddddddddddd

Read the entire file

This is a sample to read the entire file with the fread function. Use the ftell function and the fseek function to get the size of the file. , calloc function allocates memory and reads the entire file.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main (void) {
  // Open the file
  const char * in_file = "input.txt";
  FILE * in_fp = fopen (in_file, "r");
  if (in_fp == NULL) {
    fprintf (stderr, "Can't open file%s at%s line%d\n", in_file, __FILE__, __LINE__);
    exit (1);
  }
  
  // Move the position specifier to the end of the file
  fseek (in_fp, 0, SEEK_END);
  
  // Get the position of the end of the file. This will be phi size.
  size_t file_size = (int32_t) ftell (in_fp);
  
  // Allocate memory
  char * buffer = calloc (file_size + 1, sizeof (char));

  // Move the position specifier to the beginning of the file
  fseek (in_fp, 0, SEEK_SET);
  
  // Read the file
  size_t read_count = fread (buffer, sizeof (char), file_size, in_fp);
  if (read_count! = file_size) {
    fprintf (stderr, "Can't read file%s at%s line%d\n", in_file, __FILE__, __LINE__);
    exit (1);
  }
  
  // Check the contents of the file
  printf("%s\n", buffer);
  
  // Close the file
  fclose (in_fp);
}

This is an input file.

Hello
World!

This is the output result.

Hello
World!

Associated Information