RAD Studio |  |

creatnew

Header File

io.h

Category

Input/output Routines

Prototype

int creatnew(const char *path, int mode);

Description

Creates a new file.

creatnew is identical to _rtl_creat with one exception: If the file exists, creatnew returns an error and leaves the file untouched.

The mode argument to creatnew can be zero or an OR-combination of any one of the following constants (defined in dos.h):

FA_HIDDEN | Hidden file
FA_RDONLY | Read-only attribute
FA_SYSTEM | System file

Return Value

Upon successful completion, creatnew returns the new file handle, a nonnegative integer; otherwise, it returns -1.

In the event of error, the global variable errno is set to one of the following values:

EACCES | Permission denied
EEXIST | File already exists
EMFILE | Too many open files
ENOENT | Path or file name not found

Example

Copy Code

    #include <string.h>
    #include <stdio.h>
    #include <errno.h>
    #include <io.h>
    int main(void)
    {
       int handle;
       char buf[11] = "0123456789";
       /* attempt to create a file that doesn't already exist */
       handle = creatnew("DUMMY.FIL", 0);
    if (handle == -1)
          printf("DUMMY.FIL already exists.\n");
       else
       {
          printf("DUMMY.FIL successfully created.\n");
          write(handle, buf, strlen(buf));
          close(handle);
       }
       return 0;
    }

Portability

POSIX | Win32 | ANSI C | ANSI C++
| + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!