C FilesLesson 33Free preview
C Write To Files
This lesson is part of C · All levels
Summary
Writing files is done with the function fprintf (writing formatted text) or fputs after the file is opened with "w" or "a" mode. The "w" mode creates a new file or overlaps the entire contents of an existing file, while the "a" adds text at the end of the file. Always check the results of fclose or write operations to make sure the data is actually stored. After writing, the file must be closed so that the empty buffer is written to the disk.
Key points
- Write formatted text to files, not to screens
- Mode "w" overlaps the contents of the old file; mode "a" adds at the end
- Fputs(str, f) write unformatted strings to files
- Buffer files are loaded to the disk when fclose is called
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.