Block 2 · Unit 8File Handling15 min · Section 7 of 8
8.7Random Access and Positioning the File Pointer
Covers IGNOU MCS-201 §8.4, 8.5
So far every program has read files from the beginning to the end. Sometimes you need record number 500 without reading the 499 before it, or you need to change one record in place. That is random access.
Sequential versus random access
Sequential access
Random (direct) access
How data is reached
In order, from the beginning
Directly at any position
Analogy
A cassette tape
A music playlist where you tap any song
C functions
Read and write functions only
Plus fseek, ftell, rewind
Best for
Processing every record (reports, copying)
Searching or updating individual records
File type
Text or binary
Usually binary files with fixed-size records
The file position indicator
Every open stream has a file position indicator: the byte number (counting from 0) where the next read or write will happen. Each read or write moves it forward automatically. Random access means moving it yourself.
Function
Prototype
Purpose
fseek
int fseek(FILE *fp, long offset, int origin);
Move the position to offset bytes from origin; returns 0 on success, non-zero on failure
ftell
long ftell(FILE *fp);
Return the current position, or −1L on error
rewind
void rewind(FILE *fp);
Go back to the beginning (also clears the error indicator)
The origin argument is one of three macros:
Origin
Offset counted from
Example
Meaning
SEEK_SET
The beginning
fseek(fp, 10L, SEEK_SET)
Go to byte 10
SEEK_CUR
The current position
fseek(fp, -2L, SEEK_CUR)
Go back 2 bytes
SEEK_END
The end of the file
fseek(fp, -2L, SEEK_END)
Go to 2 bytes before the end
Their values are 0, 1 and 2 on common systems, but always use the names. For text files, the standard only guarantees fseek to the beginning, to the end, or to a position previously returned by ftell, because newline translation can change byte counts; use binary mode for byte-exact positioning.
position of record n=n×sizeof(record)(n=0,1,2,…)
Updating a record in place
To change one record: open the file with "r+b", read records until the right one is found, move back by one record with fseek(fp, -(long)sizeof st, SEEK_CUR), and write the modified record over the old one.
Key takeaways
Sequential access reads in order; random access jumps to any position.
fseek moves the position relative to SEEK_SET, SEEK_CUR or SEEK_END.
ftell gives the current position; rewind returns to the start.
Update a record by reading it, seeking back one record and writing it again, in a file opened with "r+b".
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain why the update program opens the file with "r+b" and why it calls fseek before fwrite.
Show worked answer
"r+b" opens an existing binary file for both reading and writing without erasing it; "w+b" would destroy the records, and "a+b" would force every write to the end of the file. After fread reads the matching record, the position is already at the start of the next record, so fseek(fp, -(long)sizeof st, SEEK_CUR) moves back by one record; the following fwrite then overwrites the matching record instead of the one after it.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/file-handling/random-access-and-positioning-the-file-pointer/ to read this page.