How to Write to File in C

Jinku Hu Feb 02, 2024
  1. Use the fwrite Function to Write to File in C
  2. Use the write Function to Write to File in C
How to Write to File in C

This article will demonstrate multiple methods about how to write to file in C.

Use the fwrite Function to Write to File in C

The standard I/O library in C provides core functions for reading/writing the files, namely fread and fwrite. fwrite takes four arguments, a void pointer where the data should be obtained from, size and number of data elements at the given pointer, and a FILE* pointer for the output file stream.

Note that the file stream should be opened first with the fopen function, which takes the filename with the desired mode and returns FILE*. In this case, we use w+ mode to open the file for reading and writing.

Mind though, if the file does not exist, a new one is created with the given name. Next, we will check if the returned file stream pointer is valid. If not, it will print the corresponding error message and exit the program.

Once the file stream pointer is verified, then the fwrite function can be called. Keep in mind that you should close the file with the fclose function before the program exits.

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

const char* str = "Temporary string to be written to file!";

int main(void) {
  const char* filename = "out.txt";

  FILE* output_file = fopen(filename, "w+");
  if (!output_file) {
    perror("fopen");
    exit(EXIT_FAILURE);
  }

  fwrite(str, 1, strlen(str), output_file);
  printf("Done Writing!\n");

  fclose(output_file);
  exit(EXIT_SUCCESS);
}

Use the write Function to Write to File in C

Alternatively, we can use write, which is a POSIX compliant function call that writes the given number of bytes to the file referred to by the file descriptor.

Note that a file descriptor is an integer number associated with the opened file streams. To retrieve the descriptor, we should call the open function with the filename path. The write function takes the file descriptor as the first argument and the buffer of the data pointed by the void* as the second one. The third argument is the number of bytes to be written to the file, calculated with the strlen function in the following example.

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

const char* str = "Temporary string to be written to file!";

int main(void) {
  const char* filename = "out.txt";

  int fd = open(filename, O_WRONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }

  write(fd, str, strlen(str));
  printf("Done Writing!\n");

  close(fd);
  exit(EXIT_SUCCESS);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C File