From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,300 courses taught by industry experts.

Writing to a file

Writing to a file

- [Instructor] To write to or create a file, you use the fopen function. The first argument is a file name string. The second argument, the mode, is either W for writing to a file or creating a new file for writing, or A for appending to a file that already exists. When the file doesn't exist, it's created and opened for writing. As with opening a file for reading, The fopen function returns a file handle, which is used with further file access functions. Data is written to a file using file versions of standard C output functions. These include fprint, fputc, fputs, and so on. These functions work like their standard IO counterparts, but with a file handle argument that indicates which open file the data is to be written to. When file writing is done, the fclose function closes the file. Do not forget this step. One of the duties of the fclose function is to complete writing data to the file, as some of the information may still be sitting in a buffer before it's actually written…

Contents