1. Technology

C Programming Tutorial - Random Access File Handling


1 of 9

Programming Random Access File I/O in C
This lesson is about using random access files in C and the following lesson will look at working with text files. Apart from the simplest of applications, most programs have to read or write files. Maybe it's just for reading a config file, or a text parser or something more sophisticated. The basic file operations are
  • fopen - open a file- specify how its opened (read/write) and type (binary/text)
  • fclose - close an opened file
  • fread - read from a file
  • fwrite - write to a file
  • fseek/fsetpos - move a file pointer to somewhere in a file.
  • ftell/fgetpos - tell you where the file pointer is located.

There are two fundamental types of file: text and binary. Of these two, binary are generally the simpler to deal with. As doing random access on a text file isn't something you need to do too often, we'll stick with binary files for the rest of this lesson. The first four operations listed above are for both text and random access files. The last two just for random access.

Random access means we can move to any part of a file and read or write data from it without having to read through the entire file. Back thirty years ago, much data was stored on large reels of computer tape. The only way to get to a point on the tape was by reading all the way through the tape. Then disks came along and now we can read any part of a file directly.

On the next page : Programming with Binary Files

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. C Programming Tutorial - Random Access File Handling

©2014 About.com. All rights reserved.