File Operations in C
File operations follow four steps every time:- Declare a file pointer
- Open the file with the appropriate mode
- Perform operations (read/write)
- Close the file
1. Writing to a File
write.txt is created in the same directory containing the two lines.
2. Reading from a File
3. Appending to a File
4. Character-by-Character Read
EOF (End Of File) is a constant that signals no more data is available.
Summary of Modes
| Need | Mode |
|---|---|
| Only read an existing file | "r" |
| Create/overwrite a file | "w" |
| Add data to end of file | "a" |
| Read and write existing file | "r+" |
| Read and write (create/clear) | "w+" |