2.2.5.1.13 file::Seek

Description

Moves the pointer to the current file position as specified. Seek enables random access within a file.

Syntax

LONG Seek( LONG lOffset, UINT nFrom )

Parameters

lOffset
[input] Number of bytes to move the file pointer relative to and in the direction indicated by nFrom.
nFrom
[input] Specifies the referenece position and direction to move the pointer to the current file position.
file::begin Moves the current file position lOffset bytes forward from the beginning of the file.
file::current Moves the current file position lOffset bytes from the current position in the file.
file::end Moves the current file position lOffset bytes backward from the end of the file. The value of lOffset must be negative otherwise the current file position will be outside the file.

Return

If the current file position is valid, seek returns the number of bytes from the beginning of the file otherwise an exception is thrown.

Examples

EX1

void file_Seek_ex1()
{
	file ff("C:\\float.txt", file::modeCreate | file::modeWrite);
	vector<float> vf(5);
	for ( int ii = 0; ii < vf.GetSize(); ii++ )
		vf[ii] = ii*1.25;
	ff.WriteFloat(vf, sizeof(float), vf.GetSize());

    LONG lCurrent = ff.Seek(20, file::begin);    // lCurrent = 20.
    lCurrent = ff.Seek(16, file::current);        // lCurrent = 20 + 16 = 36.
    lCurrent = ff.Seek(-16, file::end);            // lCurrent = length of file -16 = 4.    
    ff.Close();
}

Remark

See Also

file::GetPosition, file::SeekToBegin, file::SeekToEnd

Header to Include

origin.h