In a file handling program we must specify why and how we want to open it. In other words we must specify whether we want to create a new file, or are we overwriting an existing file, or we want to append data in an existing file. To specify it we must use different modes which are provided by C. These modes are usually in single letter like "r", "b", "w", "a". Sometimes a "+" is also used with these modes to provide some additional functionality.
- The "r" mode - Opens the file for reading. This fails if the file does not exist or cannot be found. The file to be read must be an existing file.
- "w" - Opens the file as an empty file for writing. If the file exists, its contents are destroyed.
- "a" - Opens the file for writing at the end of the file (appending) without deleting its existing contents. If the file is not existing, this mode can create a new file also.
A "+" sign can also be used with these mode to make them even more useful.
- "r+" Opens the file for both reading and writing. (The file must exist.)
- "w+" Opens the file as an empty file for both reading and writing. If the file exists, its contents are destroyed.
- "a+" Opens the file for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.
Mode Type of file Read Write Create Truncate
- r text Read
- rb+ binary Read
- r+ text Read Write
- r+b binary Read Write
- rb+ binary Read Write
- w text Write Create Truncate
- wb binary Write Create Truncate
- w+ text Read Write Create Truncate
- w+b binary Read Write Create Truncate
- wb+ binary Read Write Create Truncate
- a text Write Create
- ab binary Write Create
- a+ text Read Write Create
- a+b binary Write Create
- ab+ binary Write Create
0 comments:
Post a Comment