Pages

Thursday, December 9, 2010

Random Access in a file

Files in a computer system are accessed in two ways

1. Sequentially : This is the default behavior of the system. Sequential access is the method in which the data of the file is accessed in a serialized way. If the program is to read a file, the data will be read from the first char or byte to the second byte and so on. For example , if the file contains the data "abcdefghi", "a" will be accessed the first, followed by "b", "c" and so on. Similarly if the program is writing the data in the file then it will first write "a", followed by "b","c" and so on. The sequential file access method is fine  for reading a file which will make sense only if the data is read sequentially. The disadvantage of this method is that if you want read a particular portion of the file, you can not jump to a specific point(position) and start reading from there. In the same way, if you want to write data at a particular position you can not do it since sequential access does not allow writing at any random position.

2. Randomly : Random access is the method whereby a file can be read from or written to any position in the file. This method is quite useful where we want to skip some data and read only the desired part. Take the program of any media player for example, when you are watching a movie and you want to move forward some scenes that you have already seen, you can easily move the given "slider" and drop it at the point from where you want to continue watching. This is very useful method during file management because you don't to waste your time and memory in traversing the data that you don't want to see. Random access file handling only accesses the file at the point at which the data should be read or written, rather than having to process it sequentially.

Random File Handling Function

To handle a file randomly the function used are

1. rewind() – The "rewind()" function positions the file pointer at the beginning of the file. It does not matter where the pointer is while using this function. The "rewind()" function will place the pointer to byte no. 0 (beginning) even if it was at the end, middle or anywhere else.
Syntax
    rewind(filepointer);
Example   
    rewind(fp);

2. fseek() – To move to any particular position in a file "fseek()" is used. This function can move the cursor at any desired location.
Syntax
    fseek(file pointer, bytes to move,start position or origin)
 Where    
    file pointer - is the file in which we want to access the data randomly.
    bytes to move - is the number of bytes that we want to move. It is given in "long" format.
    Start Position or origin - The point from where we want to move. It can take the following parameters
        0 - Move from beg. of the file
        1 - Move from the current position
        2 - Move from the end of the file.


    For example if want to move to the 5th byte from the beginning, the "fseek()" function will look like
        fseek(fp,5,0); // 0 denotes that we want to move from the beginning.

        Similarly to move 20 bytes from the beginning, we can use   
        fseek(fp,20,0);

    If we want the movement to happen from the current position of the cursor, we will change the third argument to 1
        fseek(fp,10,1); // Will move 10 bytes forward from the current position.
    to move backwards, the second argument will a negative number

        fseek(fp,-30,1) ;// Will move 30 bytes backwards from the current position

    To move from the end, we will write 2 as the third argument, while the second argument will be a negative number.
        fseek(fp,-25,2);//Will move 25 bytes backwards from the end of the file

    As you can see, when we are moving from the beginning of the file, the second argument should be a positive number since you can not move backwards from the beginning of the file. In the same way if you are making a move from the end, the second argument will always be a negative number since you can not move forward from the end. The only exception is while moving from the current position. While moving from the current position you can write both positive and negative values since you can make a move in both the directions.   
3. ftell() – If we want to check the current offset (cursor) position in a file, we can use the "ftell()" function. This function returns the current position of the cursor.
Syntax
    int ftell(filepointer);
Example
    int pos;
    pos=ftell(fp);

   
    If the "ftell()" function is applied right after opening the file, it will return 0 since we haven't read the data or used the "fseek()" function.

0 comments:

Post a Comment