_FSeek( ) API Library Routine
Moves the file pointer to a new location as specified by position and mode.
long _FSeek(FCHAN chan, long position, int mode)
FCHAN chan; /* File channel. */
long position; /* Position in file. */
int mode; /* How to determine new location */
Remarks
If mode is 0 (absolute), the file pointer is set to the value of position. If mode is 1, (relative to file pointer), the value of position is added to the current file pointer position. If mode is 2 (relative to the end of the file), _FSeek( ) moves the file pointer past the end of the file. _FSeek( ) returns the new position of the file pointer. For example, _FSeek(chan, 0L, 2) moves the file pointer to the end of the file and returns the file length in bytes.
For more information on how to create an API library and integrate it with Visual FoxPro, see Accessing the Visual FoxPro API.
Example
The following example consists of one API routine, which takes two parameters, a file name, and an integer. It opens the file. Using _FSeek( ) with the FS_FROMBOF flag, it then moves the file pointer to the offset position specified by the integer parameter, and reads a single byte from the file at that position.
Visual FoxPro Code
SET LIBRARY TO FSEEK
fc = FCREATE("x", 0)
= FPUTS(fc, "abcdefghijklmnopqrstuvwxyz", 26)
= FCLOSE(fc)
? XFSEEK("x", 2) && displays 3rd byte of file x as an integer
? XFSEEK("x", 4) && displays 5th byte of file x as an integer
DELETE FILE x
C Code
#include <pro_ext.h>
FAR Example(ParamBlk FAR *parm)
{
FCHAN fchan;
char x;
// Null terminate file name
if (!_SetHandSize(parm->p[0].val.ev_handle,
parm->p[0].val.ev_length+1))
{
_Error(182); // "Insufficient memory"
}
_HLock(parm->p[0].val.ev_handle);
((char FAR *) _HandToPtr(parm->p[0].val.ev_handle))
[parm->p[0].val.ev_length] = '\0';
if ((fchan=_FOpen((char FAR*)_HandToPtr(parm->p[0].val.ev_handle),
FC_NORMAL)) < 0)
{
_UserError("Could not open file.");
}
_HUnLock(parm->p[0].val.ev_handle);
_FSeek(fchan, parm->p[1].val.ev_long, FS_FROMBOF);
_FRead(fchan, &x, 1);
_RetInt(x, 10);
_FClose(fchan);
}
FoxInfo myFoxInfo[] = {
{"XFSEEK", (FPFI) Example, 2, "C,I"},
};
FoxTable _FoxTable = {
(FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
See Also
_FCopy( ) API Library Routine | _FFlush( ) API Library Routine | _FGets( ) API Library Routine | _FPuts( ) API Library Routine