RAD Studio |  |

_sopen, _wsopen

Header File

fcntl.h, sys\stat.h, share.h, io.h, stdio.h

Category

Input/output Routines

Prototype

int _sopen(char *path, int access, int shflag[, int mode]);

int _wsopen(wchar_t *path, int access, int shflag[, int mode]);

Description

Opens a shared file.

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

_wsopen is the Unicode version of _sopen. The Unicode version accepts a filename that is a wchar_t character string. Otherwise, the functions perform identically.

For _sopen, access is constructed by ORing flags bitwise from the following lists:

Read/write flags

You can use only one of the following flags:

O_RDONLY | Open for reading only.
O_WRONLY | Open for writing only.
O_RDWR | Open for reading and writing.

Other access flags

You can use any logical combination of the following flags:

O_NDELAY | Not used; for UNIX compatibility.
O_APPEND | If set, the file pointer is set to the end of the file prior to each write.
O_CREA | If 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.
O_TRUNC | If the file exists, its length is truncated to 0. The file attributes remain unchanged.
O_EXCL | Used only with O_CREAT. If the file already exists, an error is returned.
O_BINARY | This flag can be given to explicitly open the file in binary mode.
O_TEXT | This flag can be given to explicitly open the file in text mode.
O_NOINHERIT | The file is not passed to child programs.

Note: These O_... symbolic constants are defined in fcntl.h.
If neither O_BINARY nor O_TEXT is given, the file is opened in the translation mode set by the global variable _fmode.

If the O_CREAT flag is used in constructing access, you need to supply the mode argument to _sopen from the following symbolic constants defined in sys\stat.h.

S_IWRITE | Permission to write
S_IREAD | Permission to read
S_IREAD|S_IWRITE | Permission to read/write

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

SH_COMPAT | Sets compatibility mode.
SH_DENYRW | Denies read/write access
SH_DENYWR | Denies write access
SH_DENYRD | Denies read access
SH_DENYNONE | Permits read/write access
SH_DENYNO | Permits read/write access

Return Value

On success, _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

EACCES | Permission denied
EINVACC | Invalid access code
EMFILE | Too many open files
ENOENT | Path or file function not found

Example

Copy Code

     #include <io.h>
     #include <fcntl.h>
     #include <sys\stat.h>
     #include <process.h>
     #include <share.h>
     #include <stdio.h>
     #include <stdlib.h>
     int main(void)
     {
        int handle,
            handle1;
        handle = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYWR, S_IREAD);
        if      (handle == -1)
        {
          perror (sys_errlist[errno]);
          exit (1);
        }
        if (!handle)
        {
           printf("sopen failed\n");
           exit(1);
        }
        /*      Attempt sopen for write.
        */
        handle1 = sopen("c:\\autoexec.bat", O_RDONLY, SH_DENYWR, S_IREAD);
        if      (handle1 == -1)
        {
          perror (sys_errlist[errno]);
          exit (1);
        }
        if (!handle1)
        {
           printf("sopen failed\n");
           exit(1);
        }
        close (handle);
        close (handle1);
        return 0;
    }

Portability

| POSIX | Win32 | ANSI C | ANSI C++
_sopen |  | + |  |
_wsopen |  | + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!