2.2 Opening Files
Open: To associate a logical program file with a physical system file.
We have two options: 1) open an existing file or 2) Create a new file, deleting any
existing contents in the physical file.
Opening a file makes it ready for use by the program
The C++ open function is used to open a file.
The open function must be supplied with (as arguments):
o The name of the physical file
o The access mode
o For new files, the protection mode
The value returned by the open is the fd, and is assigned to the file variable.
Function to open a file:
fd = open(filename,flags[,pmode]);
fd-file descriptor
A cardinal number used as the identifier for a logical file by operating systems such as
UNIX and PC-DOS.
For handle level access, the logical file is declared as an int.
The handle is also known as a file descriptor.
Prototypes:
int open (const char* Filename, int Access);
int open (const char* Filename, int Access, int Protection);
Example:
int Input;
Input = open ("Daily.txt", O_RDONLY);
The following flags can be bitwise ored together for the access mode:
O_RDONLY : Read only
O_WRONLY : Write only
O_RDWR : Read or write
O_CREAT : Create file if it does not exist
O_EXCL : If the file exists, truncate it to a length of zero, destroying its
contents. (used only with O_CREAT)
O_APPEND : Append every write operation to the end of the file
O_TRUNC : Delete any prior file contents
Pmode- protection mode
The security status of a file, defining who is allowed to access a file, and which access
modes are allowed.
Supported protection modes depend on the operating system, not on the
programming language.
DOS supports protection modes of:
o Read only
o Hidden
o System
for all uses of the system.
UNIX supports protection modes of:
o Readable
o Writable
o Executable
for users in three categories:
o Owner (Usually the user who created the file)
o Group (members of the same group as the owner)
o World (all valid users of the system)
Windows supports protection modes of:
o Readable
o Modifiable
o Writable
o Executable
for users which can be designated individually or in groups.:
In Unix, the pmode is a three digit octal number that indicates how the file can be used by
the owner(first digit), by members of the owner’s group(second digit), and by everyone
else(third digit).
We have two options: 1) open an existing file or 2) Create a new file, deleting any
existing contents in the physical file.
Opening a file makes it ready for use by the program
The C++ open function is used to open a file.
The open function must be supplied with (as arguments):
o The name of the physical file
o The access mode
o For new files, the protection mode
The value returned by the open is the fd, and is assigned to the file variable.
Function to open a file:
fd = open(filename,flags[,pmode]);
fd-file descriptor
A cardinal number used as the identifier for a logical file by operating systems such as
UNIX and PC-DOS.
For handle level access, the logical file is declared as an int.
The handle is also known as a file descriptor.
Prototypes:
int open (const char* Filename, int Access);
int open (const char* Filename, int Access, int Protection);
Example:
int Input;
Input = open ("Daily.txt", O_RDONLY);
The following flags can be bitwise ored together for the access mode:
O_RDONLY : Read only
O_WRONLY : Write only
O_RDWR : Read or write
O_CREAT : Create file if it does not exist
O_EXCL : If the file exists, truncate it to a length of zero, destroying its
contents. (used only with O_CREAT)
O_APPEND : Append every write operation to the end of the file
O_TRUNC : Delete any prior file contents
Pmode- protection mode
The security status of a file, defining who is allowed to access a file, and which access
modes are allowed.
Supported protection modes depend on the operating system, not on the
programming language.
DOS supports protection modes of:
o Read only
o Hidden
o System
for all uses of the system.
UNIX supports protection modes of:
o Readable
o Writable
o Executable
for users in three categories:
o Owner (Usually the user who created the file)
o Group (members of the same group as the owner)
o World (all valid users of the system)
Windows supports protection modes of:
o Readable
o Modifiable
o Writable
o Executable
for users which can be designated individually or in groups.:
In Unix, the pmode is a three digit octal number that indicates how the file can be used by
the owner(first digit), by members of the owner’s group(second digit), and by everyone
else(third digit).