Создать и открыть файл (на низком уровне)
#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 );
- path
- The path of the file you want to open.
- mode
- The access permissions that you want to use. For more information, see Access permissions.
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>
.
Файловый дескриптор. Если возникла ошибка функция возвращает -1
, код ошибки записывается в errno.
- EACCES
- Indicates one of the following permission problems:
- Search permission is denied for one of the components in the path.
- The file specified by path exists, and the permissions specified by mode are denied.
- The file specified by path doesn't exist, and the file couldn't be created because write permission is denied for the parent directory.
- EBADFSYS
- While attempting to open path, the file itself or a component of its path prefix was found to be corrupted. A system failure — from which no automatic recovery is possible — occurred while the file was being written to or while the directory was being updated. The filesystem must be repaired before proceeding.
- EBUSY
- The file specified by path is a block special device that's already open for writing, or path names a file on a filesystem mounted on a block special device that is already open for writing.
- EINTR
- The call to creat() was interrupted by a signal.
- EISDIR
- The file specified by path is a directory and the file creation flags specify write-only or read/write access.
- ELOOP
- Too many levels of symbolic links.
- EMFILE
- This process is using too many file descriptors.
- ENAMETOOLONG
- The length of path exceeds
PATH_MAX
, or a pathname component is longer than NAME_MAX
. - ENFILE
- Too many files are currently open in the system.
- ENOENT
- Either the path prefix doesn't exist, or the path argument points to an empty string.
- ENOSPC
- The directory or filesystem that would contain the new file doesn't have enough space available to create a new file.
- ENOSYS
- The creat() function isn't implemented for the filesystem specified by path.
- ENOTDIR
- A component of the path prefix isn't a directory.
- EROFS
- The file specified by path resides on a read-only filesystem.
#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 )
{
close( filedes );
return (EXIT_SUCCESS);
}
return (EXIT_FAILURE);
}
creat() — POSIX 1003.1; creat64() — Поддержка больших файлов
- Точка остановки потока
- Да
- Обработчик прерываний
- Нет
- Обработчик сигналов
- Да
- В потоке
- Да
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()
Предыдущий раздел: Описание API системной библиотеки