Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo 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\stat.h>
 #include   <io.h>                       Required for declarations only

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

    open() opens the file named in 'pathname'.  The 'oflag' argument
    specifies how the file is to be prepared for reading or writing;
    'oflag' is a combination of one or more of the constants (defined in
    fcntl.h) listed below.  To specify multiple constants, join them with
    the bitwise OR operator.

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

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

         O_EXCL     Not used; for UNIX compatibility.

       O_NDELAY     Not used; for UNIX compatibility.

       O_RDONLY     Open file for reading only.

         O_RDWR     Open file for both reading and writing. (If this flag
                    is specified, neither O_RDONLY nor O_WRONLY can 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. (Cannot 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' can be set with 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, if successful.  On
                    error, -1 is returned and 'errno' is set to one of
                    the following:

        EACCES:     Pathname is a directory or an attempt was made to
                    open a read-only file for writing.  (In DOS 3.0 and
                    above, this can also indicate a sharing violation
                    occurred.)
        EMFILE:     No more handles available (too many open files).
        ENOENT:     File or pathname not found.
       EINVACC:     Invalid access code.

      Notes:    If 'pathname' already exists, O_TRUNC will completely
                destroy the existing file; 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\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,
                         S_IREAD|S_IWRITE)) == -1)
                {
                    perror("Can't open file for output");
                    exit(1);
                }
           }


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

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