From the course: C: Data Structures, Pointers, and File Systems

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Writing to a file

Writing to a file - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Writing to a file

- [Instructor] To write to a file, you use the file open function, fopen, just as you would for opening a file to read, but instead, the file is open for writing, or creation. The first argument is a filename string. The second argument is the mode, which is w for writing or a for appending to a file that already exists. When a is specified and the file doesn't exist, the file is created and opened for writing. Once open, you use file versions of the standard output functions to send data to a file. Here are some of the functions which operate like their standard output counterparts, though they have a file handle argument. And when you're done, you use the fclose function to close the file. Do not forget this step. One of the duties of the fclose function is to finish writing data to the file, as some of the information may sit in a buffer before it's actually written. This code outputs a single line of text to a file, output.txt which it creates. The w mode shown in line eight…

Contents