Describe file management. And List the various file management functions.
- In real life, we want to store data permanently so that later on we can retrieve it and reuse it.
- A file is a collection of bytes stored on a secondary storage device like hard disk, pen drive, and tape.
- There are two kinds of files that programmers deal with text files and binary files.
- Text files are human readable and it is a stream of plain English characters.
- Binary files are not human readable. It is a stream of processed characters and Ascii symbols.
File Opening Modes
- We want to open files for some purpose like, read files, create new files, append files, read and write files, etc.
- When we open any file for processing, at that time we have to give file opening mode.
- We can do limited operations only based on the mode in which the file is opened.
e.g. fp = fopen(“demo.txt”, ”r”); //Here file is opened in read only mode.
C has 6 different file opening modes for text files
Mode |
Description |
r |
open for reading only |
w |
open for writing (If file exists then it is overwritten) |
a |
open for appending (If file does not exist then it creates new file) |
r+ |
open for reading and writing, start at beginning |
w+ |
open for reading and writing (overwrite file) |
a+ |
open for reading and writing, at the end (append if file exists) |
Same modes are also supported for binary files by just adding b, e.g. rb, wb, ab, r+b, w+b, a+b.