CONTENTS | INDEX | PREV | NEXT
open
NAME
open - open a file
SYNOPSIS
#include <fcntl.h>
int fd = open(name, modes);
char *name;
int modes;
FUNCTION
Open a file of the specified name using the specified modes. The
modes combinations yield different results as described below:
O_RDONLY open file for reading only
O_WRONLY open file for writing only
O_RDWR open file for reading and writing
O_NDELAY open file non-blocking (not implemented)
O_APPEND open file for writing only and force all writes to
append to the file regardless of the current seek position.
O_CREAT create the file if it DOES NOT exist
O_TRUNC truncate the file if it DOES exist
O_EXCL used only with O_CREAT, if the file already exists the
open will fail
O_BINARY open file for binary reading and writing, vs text. This
flag is ignored by DICE since there is no difference on
the Amiga. However, on IBM systems CR-LF must be
converted to an LF when reading text files.
open returns a descriptor (>= 0) or error (< 0) on failure.
NOTE
refer to the file_descriptor manual page for general information
Unlike file pointers and file handles, the file descriptor is
checked for validity and will simply return an error if illegal.
EXAMPLE
#include <fcntl.h>
#include <assert.h>
main()
{
int fd;
fd = open("T:xx", O_WRONLY|O_CREAT|O_TRUNC);
assert(fd >= 0);
close(fd);
fd = open("T:xx", O_CREAT|O_EXCL|O_TRUNC|O_WRONLY);
assert(fd < 0); /* should fail, file already exists */
remove("T:xx");
fd = open("T:xx", O_CREAT|O_TRUNC|O_WRONLY);
assert(fd >= 0); /* should work */
write(fd, "FuBarn", 6);
close(fd);
fd = open("T:xx", O_APPEND|O_WRONLY);
assert(fd >= 0);
write(fd, "BxBarn", 6);
close(fd);
return(0);
}
1> sampleprg
1> type t:xx
FuBar
BxBar
1>
INPUTS
char *name; filename to open
long modes; modes to open the file with
RESULTS
int fd; A file descriptor if >= 0, an error if < 0.