Borland C++ reference

_creat, _dos_creat

raw OCR

Creates a new file or overwrites an existing one.

Defined in header <dos.h>

Syntax

#include <dos.h>
int _creat(const char *path, int attrib);
unsigned  _dos_creat(const  char *path,mt attrib,mt *handlep);

Portability

DOSUNIXWindowsANSI CC++ only
_creat ■■
_dos_creat ■■

Remarks

_creat and _dos_creat open the file specified by path. The file is always opened in binary mode. Upon successful file creation, the file pointer is set to the beginning of the file. _dos_creat stores the file handle in the location pointed to by handlep. The file is opened for both reading and writing.

If the file already exists, its size is reset to 0. (This is essentially the same as deleting the file and creating a new file with the same name.)

The attrib argument to _creat is an ORed combination of the one or more of following constants (defined in dos.h):

FA_RDONLYRead-only attribute
FA_HIDDENHidden file
FA_SYSTEMSystem file

The attrib argument to _dos_creat is an ORed combination of one or more of the following constants (defined in dos.h):

_A_NORMALNormal file
_A_RDONLYRead-only file
_A_HIDDENHidden file
_A_SYSTEMSystem file

Return value

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

Upon successful completion, _dos_creat returns 0.

If an error occurs, _dos_creat returns the DOS error code.

In the event of error, _creat and _dos_creat, 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 <dos.h>
#include <string.h>
#include <stdio.h>
#include <io.h>

int mainO  {
    unsigned count;
    int handle;
    char buf[ll] =  "0123456789";
       Create a 10-byte  file using _dos_creat. */
       (_dos_creat("DUMMY.FIL", _A_NORMAL,  &handle) != 0) {
        perror ("Unable to _dos_creat DUMMY.FIL");
        return 1;
    }
    if (_dos_write (handle, buf, strlen(buf), &count) != 0) {
        perror ("Unable to _dos_write to DUMMY.FIL");
        return 1;

    _dos_close (handle);
    /* Create another 10-byte  file using _creat. */
    if ((handle =: _creat ("DUMMY2.FIL", 0)) < 0) {
        perror ("Unable to _create DUMMY2.FIL");
        return 1;
    }
    if (_write (handle, buf, strlen(buf)) < 0) {
        perror ("Unable to _write to DUMMY2.FIL");
        return 1;
    }
    _close(handle) ;
    return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.

Extraction notes

  • heading `creat, dos creat` read as `_creat, _dos_creat`