Files 455
13.8 LOW LEVEL DISK I/O
In the low-level disk I/O disk operation data can not be written as character by character or with
sequence of character as it is carried in the high-level disk I/O functions. In the low-level disk I/O
functions, buffers are used to carry the read and write operations.
Buffer plays an important role in the low-level disk I/O program. The programmer needs to
declare an appropriate buffer size. The low-level disk I/O operations are more efficient and quick
than the high level disk 1/O operations.
a. Opening a file To open a file or files open () function is used. This function is defined in "io.h".
The syntax of open () is as given below.
Syntax in t open (const char *f_name, in t access, unsigned mode)
In the low-level operation a number is assigned to the file and the number is used to refer the file. If
open () returns -1, it means that the file could not be opened otherwise the file is successfully
opened.
The Table 13.3 describes the file opening modes in the low level disk I/O operations.
Table 13.3 File opening modes.
Mode
Meaning
OAPPEND
Open a file in append mode
OJWRONLY Creates a file for writing only
ORDONLY
Opens a file for writing only
ORDWR
Opens a file for read/write operations
OBINARY
Opens a file in binary mode.
OCREATE
Opens a new file for writing.
0 EXCEL
When used with 0_CREATE, if file exists it is not overwritten.
0_TEXT
Creates a text file
When 0_CREATE flag is used, it also requires one arguments described in the Table 13.4 to verify the
read/ write status of the file. These arguments are called aspermission argument The programmer need
to # include the header file "s ta t. h" and "types. h" along with nf cntl .h ."
Table 13.4 Permission argument
SJWRITE
Writing to the file allowed
SJREAD
Reading from the file allowed
b. W riting a file The w r it e () function is used to w r ite () data into the file. This function is
defined in " i o . h ". The syntax of w r it e () function is as given below.
Syntax int write (int handle, void *buf, unsigned nbyte) ;
Returns the number of bytes written or -1 if an error occurs.
C. Reading a file The read ( ) function reads a file. The syntax of read () function is as given
below.