Sunday, 18 June 2023

Random Access to Files in C

What is Random access to files in C?

We can access or way in the data stored in the file in two ways, randomly or sequentially. So far we have used only sequential or together access in our programs. For example if we want to access the forty-forth record or data then first forty-three records or data should be read sequentially or together to reach the forty-fourth record. In random access, data can be accessed and processed or prepared randomly i.e. in this case the forty-fourth record or data can be accessed directly. There is no need to read each record or data sequentially or together, if we want to access or entry a particular data or record. Random access or entry takes less time than a together or sequential access.

Random access, c language, c language basics
Random access to file image-1

C supports these functions or task for unsystematically or random access files processing:

fseek ()

ftell()

rewind()

1.1   the fseek() and ftell() functions

The file position or place indicator has to point to the desired or want position in a file before data can be read from or written or put down to there. You can use the fseek() task or function to move the file position or place indicator or measure to the spot you want to access in a file.

The syntax for the fseek() function is

        #include <stdio.h>

        Int fseek (FILE *steam, long offset, int whence);

Here stream is the file arrow or pointer associated or related with an opened file. Offset indicates or show the number of bytes from a fixed place, state by whence, that can have one of the following essential  worth or values represented by SEEK_SET, SEEK_CUR, and SEEK_END. If it is victorious, the fseek() function returns 0; otherwise, the function or task returns a nonzero value. You can find the values represented or act for by SEEK_SET, SEEK_CUR and SEEK_END in the header file stdio.h.

 

If SEEK_SET is chosen or pick as the third argument or disagreement to the fseek() task or function, the offset is counted or add up from the beginning or birth of the file and the worth or value of the offset is greater than or equal to zero. If, however, SEEK_END is picked up, then the offset or counterbalance starts from the end of the file; the worth or value of the offset or counterbalance should be negative. When SEEK_CUR is passed or move to the fseek() function, the offset is calculated from the current or present value of the file position indicator.

Some example of usage of fseek() function are:

1.       fseek(p, 10L, 0);

Origin is 0, which means that displacement or expulsion will be relative to birth or beginning of file so position pointer is trip or skipped 10 bytes forward from the beginning of the file. Since the second argument or disagreement is a long integer, so L is attached with it.

2.       Fseek(p, 8L, SEEK_SET);

Position pointer is leap or skipped 8 bytes forward from the beginning of the file.

3.       fseek (p, -5L, 1);

Position pointer is trip or skipped 5 bytes backward from the current position.

4.       Fseek(p, -6L, SEEK_END);

Position pointer is bound or skipped 6 bytes backward from the end of file.

5.       Fseek (p, 0L, 0);

This means 0 bytes are jump or skipped from the beginning of file. After this statement position or place pointer points to the beginning of file.


Computer stuff kit tricks of Topics 61.