Borland C++ reference

sopen

raw OCR

Opens a shared file.

Defined in header <fcntl.h>

Syntax

#include <fcntl.h>
#include <sys\stat.h>
#inGlude <share.h>
#include <io.h>
int sopen(char *path, int access,intshflag[,intmode]);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

sopen opens the file given by path and prepares it for shared reading or writing, as determined by access, shflag, and mode.

For sopen, access is constructed by ORing flags bitwise from the following two lists. Only one flag from the first list can be used; the remaining flags can be used in any logical combination.

List 1: Read/write flags

0_RDONLYOpen for reading only.
0_WRONLYOpen for writing only.
0_RDWROpen for reading and writing.

List 2: Other access flags

0_NDELAYNot used; for UNIX compatibility.
0_APPENDIf set, the file pointer is set to the end of the file prior to each write.
0_CREAT 0_TRUNC If the file exists, its length is truncated to 0. The fileIf the file exists, this flag has no effect. If the file does not exist, the file is created, and the bits of mode are used to set the file attribute bits as in chmod. attributes remain unchanged.
0_EXCL O_BINARY This flag can be given to explicitly open the file in binaryUsed only with 0_CREAT. error is returned. mode.If the file already exists, an
O_TEXTThis flag can be given to explicitly open the file in text mode.

These O_... symbolic constants are defined in fcntl.h.

If neither O_BINARYnor O_TEXT is given, the file is opened in the

translation mode set by the global variable _fmode.

If the 0_CREATflag is used in constructing access, you need to supply the

mode argument to sopen from the following symbolic constants defined in sysXstat.h.

Value of modeAccess permission
S_IWRITEPermission to write
SJREADPermission to read
S IREADIS IWRITEPermission to read/write

shflag specifies the type of file-sharing allowed on the file pat/z. Symbolic constants for shflag are defined in share.h.

Value of shflagWhat it does
SH_COMPATSets compatibility mode
SH_DENYRWDenies read/write access
SH_DENYWRDenies write access
SH_DENYRDDenies read access
SH_DENYNONEPermits read/write access
SH DENYNOPermits read/write access

Return value

On successful completion, sopen returns a nonnegative integer (the file handle), and the file pointer (that marks the current position in the file) is set to the beginning of the file. On error, it returns -1, and the global variable errno is set to

ENOENTPath or file function not found
EMFILEToo many open files
EACCESPermission denied
EINVACCInvalid access code

See also

Example

#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <process.h>
#include <share.h>
#include <stdio.h>

int main (void)
{
   int handle, status;
   handle = sopen( "c:\\autoexec.bat", 0_RDONLY, SH_DENYNO,  S_IREAD);
   if (handle < 0)  {
      printf ("sopen failed\n");
      exit(l);
   }
   status = access ("c:\\autoexec.bat", 6);
   if (status == 0)
      printf ("read/write access allowed\n");
   else
      printf ("read/write access not allowed\n");
   close (handle);
   return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.