Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <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 an existing
    file. If the file specified by 'pathname' does not exist, a new file
    is created and opened for writing. If the file already exists and its
    permission setting allows writing, the file is first truncated to
    zero length (destroying the previous contents) and then is opened for
    writing.

    'pmode' applies to newly created files only; the newly created file
    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, or
                -1 if an error occurs. On error, '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_CREATE and O_TRUNC values
                specified in the 'oflag' argument is equivalent to
                creat().

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

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