creat

Creates a new file, or if the specified file exists, recreates it as an empty file.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <fcntl.h> 
   
  int creat (
     const char  *path,   
     mode_t       mode);
  

Parameters

path

(IN) Points to the name of the file to create.

mode

(IN) Specifies the file access permissions (see File Access Modes and NetWare Attributes).

Return Values

If successful, returns a file descriptor, which is a non-negative integer, and opens the file in a O_WRONLY mode. If an error occurs, returns -1 and sets errno to one of the following values:

Decimal

Constant

Description

1

ENONENT

No such file. O_CREAT is not set and the named file does not exist; or O_CREAT is set and either the path prefix does not exist or the path argument points to an empty string.

5

ENOMEM

The system is unable to allocate resources.

6

EACCES

Permission denied.

7

EEXIST

The named file exists and O_CREAT and O_EXCL are set.

9

EINVAL

Invalid mode parameter.

10

ENFILE

The maximum allowable number of files is currently open in the system.

11

EMFILE

The maximum number of files are currently open.

12

ENOSPC

There is no space left on the device to create the file.

25

ENXIO

The device does not exist.

28

EIO

A physical I/O error occurred.

63

EINTR

A signal was caught during the execution of the function.

65

ENAMETOOLONG

The length of the path parameter exceeds the maximum allowed length, or a component of the path parameter is longer than the maximum name length.

67

ENOTDIR

A component specified in the path parameter is not a directory.

81

EOVERFLOW

The operation would overflow.

76

EROFS

The named file resides on a read-only file system.

Remarks

The path parameter supplies the name of the file to be created:

  • If the file exists, it is truncated to contain no data and the preceding permission setting is unchanged.

  • If the file does not exist, it is created with access permission given by the mode parameter.

If the creat function is successful, the file is opened exclusively for writing and never for reading and writing.

See Also