(octave.info)File Positioning


Prev: EOF and Errors Up: C-Style I/O Functions
Enter node , (file) or (file)node

14.2.19 File Positioning
------------------------

Three functions are available for setting and determining the position
of the file pointer for a given file.

 -- : POS = ftell (FID)
     Return the position of the file pointer as the number of characters
     from the beginning of the file specified by file descriptor FID.

     See also: Note: fseek, Note: frewind, Note:
     feof, Note: fopen.

 -- : fseek (FID, OFFSET)
 -- : fseek (FID, OFFSET, ORIGIN)
 -- : STATUS = fseek (...)
     Set the file pointer to the location OFFSET within the file FID.

     The pointer is positioned OFFSET characters from the ORIGIN, which
     may be one of the predefined variables SEEK_SET (beginning),
     SEEK_CUR (current position), or SEEK_END (end of file) or strings
     "bof", "cof", or "eof".  If ORIGIN is omitted, SEEK_SET is assumed.
     OFFSET may be positive, negative, or zero but not all combinations
     of ORIGIN and OFFSET can be realized.

     ‘fseek’ returns 0 on success and -1 on error.

     See also: Note: fskipl, Note: frewind,
     Note: ftell, Note: fopen, *note SEEK_SET:
     XREFSEEK_SET, Note: SEEK_CUR, *note SEEK_END:
     XREFSEEK_END.

 -- : SEEK_SET ()
     Return the numerical value to pass to ‘fseek’ to position the file
     pointer relative to the beginning of the file.

     See also: Note: SEEK_CUR, *note SEEK_END:
     XREFSEEK_END, Note: fseek.

 -- : SEEK_CUR ()
     Return the numerical value to pass to ‘fseek’ to position the file
     pointer relative to the current position.

     See also: Note: SEEK_SET, *note SEEK_END:
     XREFSEEK_END, Note: fseek.

 -- : SEEK_END ()
     Return the numerical value to pass to ‘fseek’ to position the file
     pointer relative to the end of the file.

     See also: Note: SEEK_SET, *note SEEK_CUR:
     XREFSEEK_CUR, Note: fseek.

 -- : frewind (FID)
 -- : STATUS = frewind (FID)
     Move the file pointer to the beginning of the file specified by
     file descriptor FID.

     ‘frewind’ returns 0 for success, and -1 if an error is encountered.
     It is equivalent to ‘fseek (FID, 0, SEEK_SET)’.

     See also: Note: fseek, Note: ftell, Note:
     fopen.

   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);


automatically generated by info2www version 1.2.2.9