1. Home
  2. Computing & Technology
  3. C / C++ / C#

C Programming Tutorial - Random Access File Handling

By David Bolton, About.com

5 of 9

Programming Random Access Files

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
In my experience with files, unless you are just creating them (use "wb") or reading them (use "rb" ) you can get away with using "w+b". This lets you create a file from scratch,

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

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C
  5. C Tutorials
  6. Programming Random Access Files

©2009 About.com, a part of The New York Times Company.

All rights reserved.