|
||||||
Sequential and Random Access File Handling in CThe C stdio fseek, ftell and rewind Functions for use with Files
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 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:
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:
The origin parameter can be one of three values:
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 C 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.
|
||||||
|
|
||||||
|
|
||||||