C FilesLesson 32Free preview
C Create Files
This lesson is part of C · All levels
Summary
Files in C are handled through a FILE-type pointer that comes from the stdio.h header. To open a file, use a fopen with the desired file name and mode, such as "r" for reading or "w" for writing; if the file cannot be opened then the fopen returns NULL. Any file that is opened must be closed with fclose to keep the changes stored and resources from leaking. The "a" mode is used to add (append) content at the end of a file without deleting old data.
Key points
- Returns the FILE pointer or the P1 pointer if it fails
- Main mode: "r" read, "w" write (pass), "a" add at the end of the file
- It is mandatory to call in to close the file that is opened.
- Always check if the fopen is worth NULL before proceeding
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.