Syntax
#include <io.h> #include <sys\stat.h> int creat(char *pathname, int pmode);Description
The permission setting pmode applies to newly created files only. The new file receives the specified permission setting after you close it for the first time. The pmode integer expression contains one or both of the constants S_IWRITE and S_IREAD, defined in <sys\stat.h>. The values of the pmode argument and their meanings are: compact break=fit.
Value
If you do not give permission to write to the file, it is a read-only file. On the OS/2 operating system, you cannot give write-only permission. Thus, the modes S_IWRITE and S_IREAD | S_IWRITE have the same results. The creat function applies the current file permission mask to pmode before setting the permissions. (See umask for more information about file permission masks.)
When writing new code, you should use open rather than creat.
Specifying a pmode of S_IREAD is similar to making a file read-only with the ATTRIB system command.
creat returns a handle for the created file if the call is successful. A return value of -1 shows an error, and creat sets errno to one of the following values:
Value
This example creates the file sample.dat so it can be read from and written to.
#include <sys\stat.h>#include <io.h> #include <stdio.h> #include <stdlib.h> int main(void) { int fh; fh = creat("sample.dat", S_IREAD|S_IWRITE); if (-1 == fh) { perror("Error in creating sample.dat"); return EXIT_FAILURE; } else printf("Successfully created sample.dat.\n"); close(fh); return 0; /**************************************************************************** The output should: Successfully created sample.dat. ****************************************************************************/ }Related Information