RAD Studio
ContentsIndex
PreviousUpNext
_creat, _wcreat

Header File 

io.h  

Category 

Input/output Routines 

Prototype 

int creat(const char *path, int amode); 

int _wcreat(const wchar_t *path, int amode); 

Description 

Creates a new file or overwrites an existing one.

Note: Remember that a backslash in a path requires '\\'.
creat creates a new file or prepares to rewrite an existing file given by path. amode applies only to newly created files. 

A file created with creat is always created in the translation mode specified by the global variable _fmode (O_TEXT or O_BINARY). 

If the file exists and the write attribute is set, creat truncates the file to a length of 0 bytes, leaving the file attributes unchanged. If the existing file has the read-only attribute set, the creat call fails and the file remains unchanged. 

The creat call examines only the S_IWRITE bit of the access-mode word amode. If that bit is 1, the file can be written to. If the bit is 0, the file is marked as read-only. All other operating system attributes are set to 0. 

amode can be one of the following (defined in sys\stat.h):

S_IWRITE 
Permission to write 
S_IREAD 
Permission to read 
S_IREAD / S_IWRITE 
Permission to read and write (write permission implies read permission) 

Return Value 

Upon successful completion, _creat returns the new file handle, a nonnegative integer; otherwise, it returns -1. 

In the event of error, the global variable errno is set to one of the following:

EACCES 
Permission denied 
ENOENT 
Path or file name not found 
EMFILE 
Too many open files 

Example

#include <sys\stat.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
   int handle;
   char buf[11] = "0123456789";
   /* change the default file mode from text to binary */
   _fmode = O_BINARY;
   /* create a binary file for reading and writing */
   handle = creat("DUMMY.FIL", S_IREAD |S_IWRITE);
   /* write 10 bytes to the file */
   write(handle, buf, strlen(buf));
   /* close the file */
   close(handle);
   return 0;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
creat 
 
 
_wcreat 
 
NT only 
 
 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!