Explain the following file functions.
(i) fgetc( ) (ii) ftell
(iii) fgets( ) (iv) rewind( )
(v) fseek (5)
Ans:
(i)getc( ): reads a character from a file. This function is used to read a character
from a file that has been opened in the read mode. For example, the statement
c=getc(fp2); would read a character from the file whose file pointer is fp2.
(ii)ftell( ): gives the current position in the file from the start. ftell takes a file
pointer and returns a number of type long, that corresponds to the current
position. For example:n=ftell(p); would give the relative offset n in bytes of the
current position. This means that n bytes have already been read (or written).
(iii)fgets( ): To read a line of characters from a file,we use the fgets() library
function. The prototype is char *fgets(char *str, int n, FILE
*fp);The argument str is a pointer to a buffer in which the input is to be
stored, n is the maximum number of characters to be input, and fp is the pointer
to type FILE that was returned by fopen() when the file was opened.
(iv) rewind( ):sets the position to the beginning of the file. It also takes a file
pointer and reset the position to the start of the file. For example:
rewind(fp);
n=ftell(fp); would assign 0 to n because file pointer has been set to the
start of the file by rewind.
(v)fseek( ):sets the position to a desired point in the file. fseek function is used
to move the file position to a desired location within the file. For example:
fseek(fp,m,0); would move the file pointer to (m+1)th byte in the file.
No comments:
Post a Comment