creat() Create a New File
#include <io.h> Required for declarations only
#include <sys\types.h>
#include <sys\stat.h>
int creat(pathname, pmode);
char *pathname; Path name of new file
int pmode; Permission setting
creat() either creates a new file or opens and truncates an existing
file. If the file specified by 'pathname' does not exist, a new file
is created and opened for writing. If the file already exists and its
permission setting allows writing, the file is first truncated to
zero length (destroying the previous contents) and then is opened for
writing.
'pmode' applies to newly created files only; the newly created file
has the indicated permission set when it is closed. 'pmode' contains
one or both of the constants S_IWRITE and S_IREAD. The value of
these constants are:
S_IWRITE Writing permitted
S_IREAD Reading permitted
S_IREAD | S_IWRITE Reading and writing permitted
Returns: A handle for the new file if the call is successful, or
-1 if an error occurs. On error, 'errno' is set to one of
the following:
EACCES Path name specifies a read only file, or
a directory
EMFILE Too many open files.
ENOENT Path name not found.
Notes: A call to open() with the O_CREATE and O_TRUNC values
specified in the 'oflag' argument is equivalent to
creat().
-------------------------------- Example ---------------------------------
The following statements open a file for reading and writing and
print an error message if unsuccessful.
#include <sys\types.h>
#include <sys\stat.h> constants defined
#include <io.h>
#include <stdlib.h> perror declared
int hndl;
main()
{
hndl = creat("data",S_IREAD | S_IWRITE);
if (hndl == -1)
perror("error opening data file");
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster