_creat, _dos_creat
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
| DOS | UNIX | Windows | ANSI C | C++ 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_RDONLY | Read-only attribute |
| FA_HIDDEN | Hidden file |
| FA_SYSTEM | System file |
The attrib argument to _dos_creat is an ORed combination of one or more of the following constants (defined in dos.h):
| _A_NORMAL | Normal file |
| _A_RDONLY | Read-only file |
| _A_HIDDEN | Hidden file |
| _A_SYSTEM | System 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`
Stable link: /3.1/dos.h/_creat/
· short form /3.1/_creat/