Create a new file.
#include <dos.h>
#include <errno.h>
unsigned _dos_creat( char *filename, unsigned attrib, int *handle );
unsigned _dos_creatnew( char*filename, unsigned attrib, int *handle );
filename | File path name | |
attrib | File attributes | |
handle | Handle return buffer |
The _dos_creat and _dos_creatnew routines create and open a new file named filename; this new file has the access attributes specified in the attrib argument. The new file's handle is copied into the integer location pointed to by handle. The file is opened for both read and write access. If file sharing is installed, the file is opened in compatibility mode.
The _dos_creat routine uses system call 0x3C, and the _dos_creatnew routine uses system call 0x5B. If the file already exists, _dos_creat erases its contents and leaves its attributes unchanged; however, the _dos_creatnew routine fails if the file already exists.
If successful, both routines return 0. Otherwise, they return the DOS error code and set errno to one of the following values:
Constant | Meaning |
EACCES | Access denied because the directory is full or, for _dos_creat only, the file exists and cannot be overwritten |
EEXIST | File already exists (_dos_creatnew only) |
EMFILE | Too many open file handles |
ENOENT | Path or file not found |
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
/* DCREAT.C: This program creates a file using the _dos_creat function. The
* program cannot create a new file using the _dos_creatnew function
* because it already exists.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
void main( void )
{
int fh1, fh2;
int result;
if( _dos_creat( "data", _A_NORMAL, &fh1 ) != 0 )
printf( "Couldn't create data file\n" );
else
{
printf( "Created data file.\n" );
/* If _dos_creat is successful, the _dos_creatnew call
* will fail since the file exists
*/
if( _dos_creatnew( "data", _A_RDONLY, &fh2 ) != 0 )
printf( "Couldn't create data file\n" );
else
{
printf( "Created data file.\n" );
_dos_close( fh2 );
}
_dos_close( fh1 );
}
}
Created data file.
Couldn't create data file