C Now

Handle open Function

Site Logo

Usage:

The open function is used to open a file (identified by a handle).

Prototype:

int open (const char* Filename, int Access, ...); optional third argument is: int Protection ::

Arguments:

Filename
Operating system name of the file, including path if necessary.
Access
The following flags can be bitwise ored together:
O_RDONLY
Read only
O_WRONLY
Write only
O_RDWR
Read or write
O_BINARY
Binary mode
O_TEXT
Text (ASCII) mode
O_CREAT
Create file if it does not exist
O_EXCL
Do not open existing file (used only with O_CREAT)
O_APPEND
Leave any existing file contents and set file pointer at end of file
O_TRUNC
Delete any prior file contents
Protection
Required if O_CREAT is set.  The following flags can be bitwise ored together (DOS, Windows only):
S_IREAD
Read permission
S_IWRITE
Write permission

Header Files:

io.h
(open)
errno.h
(errno values)

Returns:

>0
Handle of open file
-1
Failure

Side Effect:

Iff the open fails, the global variable errno is set to error code
EACCES
Permission denied
EINVACC
Invalid access mode
EMFILE
No file handle available
ENOENT
File or path not found

Example:

#include <io.h> 
#include <fcntl.h>
#include <errno.h>
  .  .  .
  int       Account_Master;
    .  .  .
  Account_Master = open ("accounts.dat", O_RDONLY);
  if (Account_Master >0) {
    printf ("Open succeeded.\n");
  } else {
    printf ("Open failed.\n")
    switch (errno) {
      case EACCES:  printf ("Permission denied.\n"); break;
      case EINVACC: printf ("Invalid access mode.\n"); break;
      case EMFILE:  printf ("No file handle available.\n"); break;
      case ENOENT:  printf ("File or path not found.\n"); break;
      default:      printf ("Unknown error.\n"); break;
    }
  }

C Now Home  C Now Home
Include Files Index  Include Files Index
Header io.h Index  Header io.h Index
E-Mail
Valid HTML 4.01 Transitional Valid CSS! Site Logo

copyright 2001-2006, j.h.young, revised 2/13/06