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

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

 int        creat(pathname, pmode);
 char       *pathname;                   Path name of new file
 int        pmode;                       Permission setting

    creat() either creates a new file or opens and truncates the existing
    file specified by 'pathname'.  If the file does not exist, a new file
    is created and opened for writing. If the file exists and writing is
    allowed by its permission setting, the file is truncated to zero
    length (destroying the previous contents) and is then opened for
    writing.

    'pmode' applies only to a newly created file, which has the indicated
    permission set when it is closed. 'pmode' contains one or both of the
    constants S_IWRITE and S_IREAD.  The value of these constants are:

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


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

                    EACCES      Path name specifies a read only file, or
                                a directory
                    EMFILE      Too many open files.
                    ENOENT      Path name not found.

      Notes:    A call to open() with the O_CREAT and O_TRUNC values
                specified in the 'oflag' argument is equivalent to
                creat().

 Compatibility: Using MS DOS 3.0 and later, files opened by creat() are
                always opened in compatibility mode (see sopen()).
   -------------------------------- Example ---------------------------------

    The following statements open a file for reading and writing and
    print an error message if unsuccessful.

            #include <sys\types.h>
            #include <sys\stat.h>              constants defined
            #include <io.h>
            #include <stdlib.h>                perror declared

            int hndl;

            main()
            {
               hndl = creat("data",S_IREAD | S_IWRITE);
               if (hndl == -1)
                  perror("error opening data file");
            }


See Also: chmod() dup() dup2() open() sopen()

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