Write about Random Access Files ( or ) Direct Access Files?


These are occasions, however, when we are interested in accessing only a particular part of a file. This can be achieved with the help of the functions fseek, ftell & rewind available in the I/O library.

These functions are used to access the files directly from any location .These functions are useful to move the internal file pointer from one location to another location in a file.

1. ftell:

ftell takes a file pointer and return a number of type long, that corresponds to the current position, that the file pointer is advanced from the beginning of the file.

Syntax: ftell ( file pointer);

Ex:  n = ftell(fp); 

n would give the relative offset of the current position. This means that n bytes have already been read ( or written).

2. Rewind:

Rewind takes a file pointer and resets the position to the start of the file.

Syntax: rewind ( file pointer);

Ex: rewind(fp);
                                   
3. fseek:
fseek function is used to move the file position to a desired location within the file. It takes the following form:

Syntax: fseek ( file pointer, offset, position);


Ex: fseek (fp, 30,1);

File pointer is a pointer to the file concerned, offset is a number or variable of type long and position is as integer number. The offset specifies the number of positions to be moved from the location specified by position.

The position can take one of the following 3 values:

Value                               Meaning

0                                  Beginning of file
1                                  Current position
2                                  End of file

The offset may be +ve, meaning move forwards, or –ve, meaning move back words.

When the operation is successful, fseek returns a zero. If we attempt to move the file pointer beyond the file boundaries, an error occurs and fseek returns -1


Related

C Language 4266896561355122198

Post a Comment

emo-but-icon

item