_FGets( ) API Library Routine Example

The following example opens a file specified by a parameter, and copies and shows each line in the file.

Visual FoxPro Code

SET LIBRARY TO FGETS 
fc = FCREATE("x", 0)
= FPUTS(fc, REPL("X", 512), 512)
= FCLOSE(fc)
= XFGETS("x")
DELETE FILE x

C Code

#include <pro_ext.h>

#define BUFFSIZE 256
static char lineBuffer[BUFFSIZE];

void putLong(long n)
{
   Value val;

   val.ev_type = 'I';
   val.ev_long = n;
   val.ev_width = 10;

   _PutValue(&val);
}

FAR Example(ParamBlk FAR *parm)
{
   FCHAN fchan;

//   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);

   while (!_FEOF(fchan))
   {
      _FGets(fchan, lineBuffer, BUFFSIZE);
      _PutStr(lineBuffer); _PutChr('\n');
   }
   _FClose(fchan);
}

FoxInfo myFoxInfo[] = {
   {"XFGETS", (FPFI) Example, 1, "C"},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};