Sequential and Random Access File Handling in C

The C stdio fseek, ftell and rewind Functions for use with Files

Nov 16, 2009 Guy Lecky-Thompson

A file handling in C tutorial detailing the use of sequential and random access files in C, along with examples of using the fseek, ftell and rewind functions.

In computer programming, the two main types of file handling are:

  • Sequential;
  • Random access.

Sequential files are generally used in cases where the program processes the data in a sequential fashion – i.e. counting words in a text file – although in some cases, random access can be feigned by moving backwards and forwards over a sequential file.

True random access file handling, however, only accesses the file at the point at which the data should be read or written, rather than having to process it sequentially. A hybrid approach is also possible whereby a part of the file is used for sequential access to locate something in the random access portion of the file, in much the same way that a File Allocation Table (FAT) works.
The three main functions that this article will deal with are:
  • rewind() – return the file pointer to the beginning;
  • fseek() – position the file pointer;
  • ftell() – return the current offset of the file pointer.

Each of these functions operates on the C file pointer, which is just the offset from the start of the file, and can be positioned at will. All read/write operations take place at the current position of the file pointer.

The rewind() Function
The rewind() function can be used in sequential or random access C file programming, and simply tells the file system to position the file pointer at the start of the file. Any error flags will also be cleared, and no value is returned.
While useful, the companion function, fseek(), can also be used to reposition the file pointer at will, including the same behavior as rewind().
Using fseek() and ftell() to Process Files
The fseek() function is most useful in random access files where either the record (or block) size is known, or there is an allocation system that denotes the start and end positions of records in an index portion of the file. The fseek() function takes three parameters:
  • FILE * f – the file pointer;
  • long offset – the position offset;
  • int origin – the point from which the offset is applied.

The origin parameter can be one of three values:

  • SEEK_SET – from the start;
  • SEEK_CUR – from the current position;
  • SEEK_END – from the end of the file.

So, the equivalent of rewind() would be:

fseek( f, 0, SEEK_SET);
By a similar token, if the programmer wanted to append a record to the end of the file, the pointer could be repositioned thus:
fseek( f, 0, SEEK_END);
Since fseek() returns an error code (0 for no error) the stdio library also provides a function that can be called to find out the current offset within the file:
long offset = ftell( FILE * f )
This enables the programmer to create a simple file marker (before updating a record for example), by storing the file position in a variable, and then supplying it to a call to fseek:

long file_marker = ftell(f);

// … file processing functions

fseek( f, file_marker, SEEK_SET);
Of course, if the programmer knows the size of each record or block, arithmetic can be used. For example, to rewind to the start of the current record, a function call such as the following would suffice:
fseek( f, 0 – record_size, SEEK_CURR);
With these three functions, the C programmer can manipulate both sequential and random access files, but should always remember that positioning the file pointer is absolute. In other words, if fseek is used to position the pointer in a read/write file, then writing will overwrite existing data, permanently.

The copyright of the article Sequential and Random Access File Handling in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Sequential and Random Access File Handling in C in print or online must be granted by the author in writing.
File Handling in C, SXC.hu File Handling in C
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 1+7?