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 - <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 specified by 'pathname'.  The 'oflag' argument
    specifies how the file is to prepared 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    Reposition file pointer to end-of-file before every
                    write operation.

        O_CREAT     Create and open a new file for writing. (No effect if
                    the file 'pathname' already exists.)

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

        O_RDONLY    Open file for reading only.

        O_RDWR      Open file for both reading and writing. (If this flag
                    is specified, neither O_RDWR nor O_WRONLY may be
                    specified.)

        O_TRUNC     Open and truncate an existing file to 0 length. The
                    file must have write permission. The contents of the
                    file are destroyed.

        O_WRONLY    Open file for writing only. (May not be specified
                    with either 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.

    The 'pmode' argument is required only when O_CREAT is used. 'pmode'
    specifies the file's permission setting. (If the file already exists,
    'pmode' is ignored.) 'pmode' is set using the following constants:

        S_IWRITE                Writing permitted
        S_IREAD                 Reading permitted
        S_IREAD | S_IWRITE      Reading and writing permitted

    Returns:    A file handle for the opened file, or -1 if an error
                occurs.  If an error occurs, 'errno' is set to one of the
                following:

        EACCES:     Pathname is a directory; or tried to open a read-only
                    file for writing (or a sharing violation occurred;
                    this can occur only in DOS 3.0 and above).
        EEXIST:     O_CREAT and O_EXCL flags are specified but the file
                    already exists.
        EMFILE:     No more handles available (too many open files).
        ENOENT:     File or path name not found.

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

  -------------------------------- 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