Condividi tramite


_FGets( ) API Library Routine

Copies to the buffer a single line from the specified file, starting at the current position in the file.

unsigned int _FGets(FCHAN chan, char FAR *buffer, int maxlen)
FCHAN chan;               /* File channel of file from which to copy. */
char FAR *buffer;            /* Buffer address. */
int maxlen;                  /* Maximum length of line in bytes. */

Remarks

_FGets( ) copies a line of maxlen length, delimited with a carriage return. The carriage return is translated to a null terminator and stored in the buffer. Line feeds are ignored and aren't copied into the buffer. _FGets( ) returns the number of bytes copied to the buffer.

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 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
};

See Also

_FRead( ) API Library Routine | _FSeek( ) API Library Routine | Accessing the Visual FoxPro API