.TH creat() "" "" "System Call (libc)"
.PC "Create/truncate a file"
.B "#include <fcntl.h>"
\fBint creat(\fIfile\fB, \fImode\^\fB)\fR
\fBchar *\fIfile\^\fB; int \fImode\^\fB;\fR
.PP
.B creat()
creates a new
.I file
or truncates an existing
.IR file .
It returns a file descriptor that identifies
.I file
for subsequent system calls.
If
.I file
already exists, its contents are erased.
In this case,
.B creat()
ignores the specified \fImode\^\fR; the mode of the
.I file
remains unchanged.
If
.I file
did not exist previously,
.B creat()
uses the
.I mode
argument to determine the mode of the new
.IR file .
For a full definition of file modes,
see
.B chmod()
or the header file
.BR stat.h .
.B creat()
masks the
.I mode
argument with the current
.BR umask ,
so it is common practice to create files with the maximal mode desirable.
.SH Example
For an example of how to use this routine, see the entry for
.BR open() .
.SH "See Also"
.Xr "chmod()," chmod
.Xr "fcntl.h," fcntl.h
.Xr "fopen()," fopen
.Xr "libc," libc
.Xr "open()," open
.Xr "stat.h," stat.h
.Xr "stdio.h" stdio.h
.br
\*(AS, \(sc4.9.3
.br
\*(PX Standard, \(sc5.3.2
.SH Diagnostics
If the call is successful,
.B creat()
returns a file descriptor.
It returns \-1 if it could not create the file, typically
because of insufficient system
resources or protection violations.
