Condividi tramite


_FOpen( ) API Library Routine

Assigns a Visual FoxPro channel to an existing file.

FCHAN _FOpen(char FAR *filename, int mode)
char FAR *filename;         /* Name of existing file */
int mode;                     /* Mode option. */

Remarks

The following mode options are available: FO_READONLY, FO_WRITEONLY, and FO_READWRITE. _FOpen( ) opens a file with exclusive access. _FOpen( ) returns the file channel if it's successful in opening the file, or returns – 1 if it can't open the file.

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 creates a test file. It then opens the file in FO_READONLY mode and attempts to write to it. As a result _FError( ) returns 5 for "Access denied." Next, the example opens the test file in FO_WRITEONLY mode, and attempts to read from it. Again, _FError( ) returns 5.

Visual FoxPro Code

SET LIBRARY TO FOPEN 

C Code

#include <pro_ext.h>

void putLong(long n)
{
   Value val;

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

   _PutValue(&val);
}

#define BUFFSIZE 256
static char lineBuffer[BUFFSIZE];

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

   fchan = _FCreate("temp.tmp", FC_NORMAL);
   _FCHSize(fchan, 8192);
   _FClose(fchan);

   fchan = _FOpen("temp.tmp", FO_READONLY);
   _FPuts(fchan, "Hello, world");
   _PutStr("\nAttempt to _FPuts() to file _FOpen()d FO_READONLY");
   _PutStr("\n_FError() ="); putLong(_FError());
   _FClose(fchan);

   fchan = _FOpen("temp.tmp", FO_WRITEONLY);
   _FGets(fchan, lineBuffer, BUFFSIZE);
   _PutStr("\nAttempt to _FGets() from file _FOpen()d FO_WRITEONLY");
   _PutStr("\n_FError() ="); putLong(_FError());
   _FClose(fchan);
}

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

See Also

_FClose( ) API Library Routine | _FCreate( ) API Library Routine | _FError( ) API Library Routine | Accessing the Visual FoxPro API