Create and open a file (low-level)
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat( const char* path,
mode_t mode );
int creat64( const char* path,
mode_t mode );
libc
The creat() and creat64() functions create and open the file specified by path with the given mode.
Calling creat() is the same as:
open( path, O_WRONLY | O_CREAT | O_TRUNC, mode );
Similarly, calling creat64() is the same as:
open64( path, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, mode );
If path exists and is writable, it's truncated to contain no data, and the existing mode setting isn't changed.
If path doesn't exist, it's created with the access permissions specified by the mode argument. The access permissions for the file or directory are specified as a combination of the bits defined in <sys/stat.h>.
A file descriptor on success, or -1 if an error occurs (errno is set).
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main( void )
{
int filedes;
filedes = creat( "file",
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
if( filedes != -1 ) {
/* process file */
close( filedes );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
creat() is POSIX 1003.1; creat64() is for large-file support
| Safety: | |
|---|---|
| Cancellation point | Yes |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
chsize(), close(), dup(), dup2(), eof(), errno, execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), fcntl(), fileno(), fstat(), isatty(), lseek(), open(), read(), sopen(), stat(), tell(), write(), umask()