Node:File Positioning, Previous:EOF and Errors, Up:C-Style I/O Functions
Three functions are available for setting and determining the position of the file pointer for a given file.
ftell (fid) | Built-in Function |
Return the position of the file pointer as the number of characters from the beginning of the file fid. |
fseek (fid, offset, origin) | Built-in Function |
Set the file pointer to any location within the file fid.
The pointer is positioned offset characters from the origin,
which may be one of the predefined variables Return 0 on success and -1 on error. |
SEEK_SET | Built-in Variable |
SEEK_CUR | Built-in Variable |
SEEK_END | Built-in Variable |
These variables may be used as the optional third argument for the
function fseek .
|
frewind (fid) | Built-in Function |
Move the file pointer to the beginning of the file fid, returning
0 for success, and -1 if an error was encountered. It is equivalent to
fseek (fid, 0, SEEK_SET) .
|
The following example stores the current file position in the variable
marker
, moves the pointer to the beginning of the file, reads
four characters, and then returns to the original position.
marker = ftell (myfile); frewind (myfile); fourch = fgets (myfile, 4); fseek (myfile, marker, SEEK_SET);