_FSeek( ) API Library Routine 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
};