This table shows all of the combinations including both text and binary files. Generally you either read from or write to a text file, but not both at the same time.
With a binary file you can both read and write to the same file. The table below shows what you can do with each combination.
- 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
Some implementations also allow other letters. Microsoft for instance allows "t" (text mode), "c" for commit, "n" for non-commit, "S" for optimizing caching for sequential access, "R" for caching non-sequential (ie random access), "T" for temporary and "D" for delete/temporary- it kills the file when it's closed. These aren't portable so use them at your own peril!
On the next page : Programming Random Access Files

