fwrite function-write to file with specified number of bytes

You can use the fwrite function to write to a file by specifying the number of bytes. You can use it by writing the stdio.h header.

#include <stdio.h>
size_t fwrite (const void * buf, size_t size, size_t n, FILE * fp);

The first argument is the buffer that contains the content you want to write. The second argument is the unit of bytes of data to write. The third argument is the number of data to write. The actual byte size written will be "size * n". The fourth argument is the file stream of the file you want to write.

The return value is the number of data written. It corresponds to the number of data specified by the third argument. Please note that it is not a byte size. If a write error occurs, the return value will be less than the number of data specified by the third argument.

Sample to write with fwrite function

This is a sample to write with the fwrite function. Open the file with fopen function, and read 4 bytes at a time with fread function. The fwrite function writes to the file 4 bytes at a time, and the fclose function closes the file.

The file is open in binary mode when reading and writing.

#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, "rb");
  if (in_fp == NULL) {
    fprintf (stderr, "Can't open file%s\n", in_file);
    exit (1);
  }

  // Open the file in write mode
  const char * out_file = "output.txt";
  FILE * out_fp = fopen (out_file, "wb");
  if (out_fp == NULL) {
    fprintf (stderr, "Can't open file%s\n", out_file);
    exit (1);
  }

  // Read and output to another file
  while (1) {
    
    // Read 4 bytes at a time
    char buffer [16];
    int32_t read_unit = 1;
    int32_t read_count = 4;
    size_t real_read_count = fread (buffer, read_unit, read_count, in_fp);
    
    // Write 4 bytes at a time
    fwrite (buffer, read_unit, real_read_count, out_fp);
    
    if (real_read_count <read_count) {
      break;
    }
  }
  
  // Close the file with the fclose function
  fclose (in_fp);
}

It is an input file "input.txt".

Hello
World!

The output file is "output.txt".

Hello
World!

Read and write the entire file

This is a sample to read and write the entire file. Use the ftell function and the fseek function to get the size of the file. , calloc function to allocate memory, and fread function to fill the entire file. Read and write with the fwrite function.

The file is open in binary mode when reading and writing.

#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, "rb");
  if (in_fp == NULL) {
    fprintf (stderr, "Can't open file%s at%s line%d\n", in_file, __FILE__, __LINE__);
    exit (1);
  }

  // Open the file in write mode
  const char * out_file = "output.txt";
  FILE * out_fp = fopen (out_file, "wb");
  if (out_fp == NULL) {
    fprintf (stderr, "Can't open file%s\n", out_file);
    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);
  }
  
  // Write to output file
  fwrite (buffer, sizeof (char), file_size, out_fp);
  
  // Close the file
  fclose (in_fp);
}

It is an input file "input.txt".

Hello
World!

The output file is "output.txt".

Hello
World!

Associated Information