C Programming: Random access files

       The functions, so for what I have presented are allow access file in sequentially only. But there may be a situation where to access a file randomly, if it is the case, the functions what we are described are not suitable
So, C provides a solution to work with random files. The C I/O library provides the following function to working with random files. They are fseek,ftell, and rewind.



The ftell() is used to save the current position of the file,which can be used later.It takes a file pointers as input and return a number of types long, that corresponds to the current position. It take the following form

                                      n=ftell(fp);
n stores the relative offset in bytes of the current.


       The rewind() function is used to reset the position of to the start of the file. It takes a file pointer as input. For example,

                           rewind(fp);
                          n=ftell(fp);
The fseek() function is used to move the file pointer to a specific location in a file. It takes the following form

                         fseek( file_pointer, offset, position);
-offset is a number or variable of type long, and position is an integer. The offset specifies the number of position to be moved from the location specified by position. The position takes one of the following three values.

                Value                   Meaning 
                  0                     Beginning of file
                  1                     Current position 
                   2                    End of file 

No comments: