C Write Text File

Summary: in this tutorial, you will learn how to write text file using C standard I/O functions.

C Write Text File Steps

The following illustrates the steps of writing text into a text file line by line:

  • First, open the file for writing using fopen() function with mode ‘w’. It is important to note that in this mode the file content is deleted and the file is overwritten if the file already exists.
  • Second, use function fprintf() to write text into the file stream.
  • Third, use function fclose() to close the file.

C Write Text File Example

The following program writes 10 lines of text into a text file named 1.txt in the c:\temp folder.

In this tutorial, you’ve learned how to write text into text file using function fprintf().