Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>_dos_open() open an existing file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 _dos_open()             Open an existing file

 #include   <dos.h>
 #include   <fcntl.h>     Defines access and inheritance codes
 #include   <share.h>     Defines sharing codes

  unsigned     _dos_open(path, mode, handle);
  char *path;  String containing drive, path, and filename
  unsigned mode;  Allowable access and use of file
  int *handle;  File identifier return buffer

    _dos_open() uses MS-DOS function 3Dh to open the existing file
    indicated by 'path'. The handle identifier is assigned by _dos_open()
    and returned in the location pointed to by 'handle'. The 'mode'
    argument specifies allowable access, sharing, and inheritance by
    ORing together up to one manifest constant from each mode in the list
    below.

                    Constant      Mode         Meaning
                    ------------------------------------------------
                    O_RDONLY      Access       Read only
                    O_WRONLY      Access       Write only
                    O_RDWR        Access       Both read and write
                    SH_COMPAT     Sharing      Compatibility
                    SH_DENYRW     Sharing      Deny reading, writing
                    SH_DENYWR     Sharing      Deny writing
                    SH_DENYRD     Sharing      Deny reading
                    SH_DENYNO     Sharing      Deny neither
                    O_NOINHERIT   Inheritance  File is not inherited
                                               by the child process

    Returns:    _dos_open() returns 0 if the file was opened
                successfully. If unsuccessful, it returns the MS-DOS
                error code and sets errno to one of the following
                manifest constants:

                    Constant     Meaning
                    ------------------------------------------------
                    EINVAL       Invalid access code, or sharing
                                 mode not allowed

                    ENOENT       Unknown path or filename

                    EMFILE       No file handles available (too
                                 many files open already)

                    EACCES       Access denied (file already open
                                 in incompatible mode, or directory
                                 or volume label was specified)

      Notes:    _dos_open() can only be used with existing files. To
                create a new file, use _dos_creat(), _dos_creatnew(), or
                creat(). When opened, the read/write pointer is set to
                the first byte of the file. To move the pointer use
                lseek().

                In MS-DOS 2.0 and 2.1 only the Access modes are
                permitted. MS-DOS 3.0 or higher is required for the
                Sharing and Inheritance modes. The MS-DOS SHARE command
                must be previously invoked to enable the Share modes.

                Use _dos_open() if you wish to share a file and do not
                need text conversions or automatic read/write pointer
                positioning, otherwise use sopen() or open(). Sopen() and
                open() cause a bug for certain argument choices when used
                in MS-DOS 3.0, 3.1, or 3.2 with SHARE installed.

                    Compatibility mode is similar to Deny None mode,
                    except that other programs must also open the file in
                    Compatibility mode. Otherwise they are restricted to
                    the read, write, or read/write Access mode specified
                    in this call.

   Portability:     MS-DOS only, version 2.0 or higher

------------------------------- Example ---------------------------------

 This program opens an existing file for shared read/write access.

           #include      <dos.h>
           #include      <fcntl.h>    Defines access and inheritance codes
           #include      <share.h>    Defines sharing codes

           main()
           {
             int handle;
             unsigned err_code;

             err_code = _dos_open("c:\work\datafile.dat", O_RDWR|SH_DENYNO,
           &handle);
             if (err_code)
                printf("Unable to open file -- error code is %u\n",
           err_code);
             .
             .  (file is used and closed)
             .
           }


See Also: _dos_creat() _dos_close()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson