Borland C++ reference

creat

raw OCR

Creates a new file or overwrites an existing one.

Defined in header <sys/stat.h>

Syntax

#include <sys\stat.h>
int creat(const char *path, intamode);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

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 is written to. If the bit is 0, the file is marked as read-only. All other DOS attributes are set to 0.

amode can be one of the following (defined in sysXstat.h):

Value of amodeAccess permission
SJWRITEPermission to write
SJREADPermission to read
S IREADIS IWRITEPermission to read and write

In DOS, write permission implies read permission.

Return value

Upon successful completion, creat returns the new file handle, a non-negative integer; otherwise, it returns -1.

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

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

See also

Example

#include <sys\stat.h>
#include <process.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <io.h>

int main (void)
{
   int handle;
   char buf [1 : '01234567S
/* create a binary  file for reading and writing */
if  ((handle = _creat("DUMMY.FIL", 0)) < 0) {
   switch (errno) {
      case ENOENT: printf ("Error: Path or file not found. \n"
                   break;
      case EMFILE: printf ("Error: Too many open files.\n");
                   break;
      case EACCES: printf ("Error: Permission denied.\n");
                   break;
      default:     printf ("Error creating file.\n");
                   break;
   }
   exit(l);
}
/* write a string  and NULL terminator into the file */
write (handle, buf, strlen(buf)+l);

/* close the  file */
close (handle);
return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.