Share via


CStdioFile::Seek

Repositions the pointer in a previously opened file.

virtual ULONGLONG Seek(
   LONGLONG lOff,
   UINT nFrom 
);

Parameters

  • lOff
    Number of bytes to move the pointer.

  • nFrom
    Pointer movement mode. Must be one of the following values:

    • CFile::begin: Move the file pointer lOff bytes forward from the beginning of the file.

    • CFile::current: Move the file pointer lOff bytes from the current position in the file.

    • CFile::end: Move the file pointer lOff bytes from the end of the file. Note that lOff must be negative to seek into the existing file; positive values will seek past the end of the file.

Return Value

If the requested position is legal, Seek returns the new byte offset from the beginning of the file. Otherwise, the return value is undefined and a CFileException object is thrown.

Remarks

The Seek function permits random access to a file's contents by moving the pointer a specified amount, absolutely or relatively. No data is actually read during the seek. If the requested position is larger than the size of the file, the file length will be extended to that position, and no exception will be thrown.

When a file is opened, the file pointer is positioned at offset 0, the beginning of the file.

This implementation of Seek is based on the Run-Time Library (CRT) function fseek. There are several limits on the usage of Seek on streams opened in text mode. For more information, see fseek, _fseeki64.

Example

The following example shows how to use Seek to move the pointer 1000 bytes from the beginning of the cfile file. Note that Seek does not read data, so you must subsequently call CStdioFile::ReadString to read data.

CStdioFile cfile(_T("Stdio_Seek_File.dat"), CFile::modeWrite |
    CFile::modeCreate);
LONGLONG lOff = 1000;
ULONGLONG lActual = cfile.Seek(lOff, CFile::begin);

Requirements

Header: afx.h

See Also

Reference

CStdioFile Class

Hierarchy Chart

CFileException Class

CStdioFile::ReadString

CFile::Read

Other Resources

CStdioFile Members