|
Seek
int Seek(string
filename, int pos, int from)
Return
Value
If the requested position
is legal, returns the new byte offset from the beginning of the
file. Otherwise, the return value is FAIL.
Parameters
filename
– the name of
the file.
pos
- number of bytes to move the pointer.
from
- pointer movement mode. Must be one of the following
values:
|
Mode
|
Description
|
|
FILE_BEGIN_POS
|
Move the file pointer by
pos
bytes forward
from the beginning of the file.
|
|
FILE_CURRENT_POS
|
Move the file pointer by
pos
bytes from the
current position in the file.
|
|
FILE_END_POS
|
Move the file pointer by pos
bytes from the end of the file. Note that pos
must be negative to seek into the existing file; positive values
will seek past the end of the file.
|
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.
Package
Plug in module:
nd_fileplg.npl
Example
The following
example demonstrates the use of Seek.
# Example for Seek
# need
to extract a name from the file
string sWriteData = "Name: Andrew"
string sFileName = "C:\test.txt"
int nPosition = 6
array ReadData
int i
string sName
if(WriteString(sFileName, sWriteData) ==
OK)
if(Seek(sFileName,
nPosition, FILE_BEGIN_POS) != FAIL)
ReadData = ReadData(sFileName, -1)
for(i=0;
i<GetArraySize(ReadData); i++)
sName += ReadData[i]
endfor
endif
endif
Println(sName)
CloseFile(sFileName)
Output of the example
script:
Andrew.
|