1. HowTo
  2. C Howtos
  3. Write to File in C

Write to File in C

Created: February-01, 2021 | Updated: February-07, 2021

  1. Use the fwrite Function to Write to File in C
  2. Use the write Function 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 <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.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); }                          

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C File

  • Get File Size in C
  • open vs fopen in C
  • Ezoic