To create a file for writing or append to an existing file, use the fopen() function with the mode options "w" or "a", respectively. A file opened for writing is created if it doesn't exist or, if it does exist, it's overwritten. File writing functions resemble the standard output functions in C, but with an extra argument for the file handle. Ensure that you close a file opened for writing so that the last bit of data is sent to the 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. …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.