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>open() open a file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 open()                  Open a File

 #include   <fcntl.h>
 #include   <sys\types.h>
 #include   <sys\stat.h>
 #include   <io.h>                       Required for declarations only

 int        open(pathname,oflag[,pmode]);
 char       *pathname;                   File path name
 int        oflag;                       Type of operations allowed
 int        pmode;                       Permission setting

    open() opens the file designated by 'pathname' and uses the 'oflag'
    argument to ready the file for reading or writing; 'oflag' is a
    combination of one or more of the following constants, defined in
    <fcntl.h>.  (To specify multiple constants, join them with the
    bit-wise OR operator (|).)

       O_APPEND     Move file pointer to end-of-file before each write
                    operation.

        O_CREAT     Create a new file and open it for writing; no effect
                    if 'pathname' already exists. (See 'pmode' discussion
                    below.)

         O_EXCL     Used only with O_CREAT to return an error value if
                    'pathname' already exists.

       O_RDONLY     Open file with read access only; cannot be used with
                    O_RDWR or O_WRONLY.

         O_RDWR     Open file with both read and write access; cannot be
                    used with O_RDONLY and O_WRONLY.

        O_TRUNC     Open an existing file and truncate to 0 length. Write
                    permission is required; file contents are destroyed.

       O_WRONLY     Open file with write access only; cannot be used with
                    O_RDONLY or O_RDWR.

         O_TEXT     Open file in text (translated) mode. In text mode,
                    CR-LF is translated to a single LF when reading; LF
                    is translated to CR-LF when writing.

       O_BINARY     Open file in binary (untranslated) mode. In
                    untranslated mode, the translation of CR-LF described
                    under O_TEXT is suppressed.

    Either O_RDONLY, O_RDWR, or O_WRONLY must be given to specify the
    access mode; there is no default access mode.

    The 'pmode' argument specifies a new file's permission settings and
    is needed only with O_CREAT.  ('pmode' is ignored for files that
    already exist.) 'pmode' is set using the following constants:

 S_IWRITE                Write permission
 S_IREAD                 Read permission
 S_IREAD | S_IWRITE      Read and write permission

    Returns:    A file handle for the opened file. On error, -1 is
                returned and 'errno' is set to one of the following:

 EACCES:     Pathname specifies a directory; file is read-only (cannot be
                    opened for writing); or a sharing violation occurred
                    (DOS 3.0 and above only).
 EEXIST:     O_CREAT and O_EXCL specified for an existing file.
 EMFILE:     No more handles available (too many open files).
 ENOENT:     File or path name not found.
 EINVAL:     An invalid 'oflag' argument was given.

      Notes:    O_TRUNC completely destroys existing files; use it with
                care.

 Portability:   Not supported by ANSI standard.

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

    This example opens an existing file for input. Upon success it
    creates a new file for output.

           #include <fcntl.h>
           #include <sys\types.h>
           #include <sys\stat.h>
           #include <io.h>
           #include <stdlib.h>

           main()
           {
                 int inhndl, outhndl;

                 if ((inhndl = open("info.dat",O_RDONLY)) == -1)
                 {
                    perror("Can't open input file");
                    exit(1);
                 }
                 else if ((outhndl = open("data.dat",O_CREAT|O_EXCL,
                           S_IREAD|S_IWRITE)) == -1)
                       {
                        perror("Can't open file for output");
                        exit(1);
                       }
           }

See Also: fopen() sopen() close() creat() dup()

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