Borland C++ reference

open

raw OCR

Opens a file for reading or writing.

Defined in header <fcntl.h>

Syntax

#include <fcntl.h>
#include <sys\stat.h>
int open(const char *path, int access [,unsigned mode]);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

open opens the file specified by path, then prepares it for reading and/or writing as determined by the value of access.

To create a file in a particular mode, you can either assign to the global

variable _fmode or call open with the 0_CREATand 0_TRUNCoptions

ORed with the translation mode desired. For example, the call

open("xmp",0_CREATI0_TRUNCI0_BINARY,S_IREAD) will create a binary-mode, read-only file named XMP, truncating its length to 0 bytes if it already existed.

For open, access is constructed by bitwise ORing flags from the following two lists. Only one flag from the first list can be used (and one must be used); the remaining flags can be used in any logical combination.

List 1: Read/write flags

These symbolic

0_RDONLYOpen for reading only.

constants are

0_WRONLYOpen for writing only.

defined in fcntl.ti.

0_RDWROpen for reading and writing.

List 2: Other access flags

0_NDELAY Not used; for UNIX compatibility. 0_APPEND If set, the file pointer will be set to the end of the file prior to each write.

0_CREATIf the file exists, this flag has no effect. If the file does not exist, the file is created, and the bits of mode are used to set the file attribute bits as in chmod.

0_TRUNC If the file exists, its length is truncated to 0. The file attributes remain unchanged.

0_EXCLUsed only with 0_CREAT. error is returned.If the file already exists, an

OBINARY Can be given to explicitly open the file in binary mode.

O_TEXTCan be given to explicitly open the file in text mode.
If neither O_BINARYnor O_TEXT is given, the file is opened in the

translation mode set by the global variable Jrnode.

If the 0_CREATflag is used in constructing access, you need to supply the

mode argument to open from the following symbolic constants defined in sysXstat.h.

Value of modeAccess permission
S_IWRITEPermission to write
S IREADPermission to read
SJREAD 1SJWRITEPermission to read and write

Return value

On successful completion, open returns a nonnegative integer (the file handle). The file pointer, which marks the current position in the file, is set to the beginning of the file. On error, open returns -1 and the global variable errno is set to one of the following:

ENOENT No such file or directory EMFILE Too many open files

EACCESPermission denied
EINVACCInvalid access code

See also

Example

#include <string.h>
#include <stdio.h>
*include <fcntl.h>
#include <io.h>

int main (void)
{
   int handle;
   char msg[] =  "Hello world";
   if ((handle = open("TEST.$$$" , 0_CREAT  I 0_TEXT)) == -1) {
      perror ("Error:");
      return 1;
   }■
   write(handle, msg,  strlen(msg));
   close(handle) ;
   return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.