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.
456 Programming and Data Structures
Syntax int read (int handle, void *buf, unsigned le n );
Upon successful end, it returns an integer specifying the number of bytes placed in the buffer; if the file
was opened in text mode, read does not count carriage returns or Ctrl - Z characters in the number of
bytes read. On error, it returns -1 and sets ermo.
d. Closing a file The close () function closes the file. This function is defined in " i o . h ." The
syntax of close () is as given below.
Syntax in t close (in t h an dle);
in t c lo s e (in t handle);
Upon successful finish, close & _close return 0; otherwise, they return -1 and set ermo.
13.35 Write a program to enter text through keyboard and store it on the disk. Use low-level
disk I/O operations.
# inclu de < s td io .h >
# inclu de <con io .h >
# in clu de < io .h>
# includ e < fc n t l.h >
void mainO
{ char f i l e [101;
char b u f f [1 5 ];
int s,c;
puts("n Enter a file name:");
gets(file);
s=open(file,0_CREAT I OJTEXT);
i f ( s ~ - l )
puts("n File does not exits");
else
I
puts( 'Enter text below : " );
for (c=0;c<=23;c++)
buff[c]-getche();
buff[ch'0';
wri te (s, buff, 15);
close(s);
I
Q-UTFUT;
Enter a file name :
TEXT
Enter text below : PROGRAMMING IN C
Files 457
Explanation In the above program a prompt appears for asking a file name to be created. A file is
created with the name entered by the user. If the file is already present it won't be overwritten and the
entered text won't be written in the file. If open () fails to open the file it will return -1 and this value
is assigned to variable s. The i f statement checks the value of s and respective blocks are executed. In
the e lse block the getche () within the fo r loop reads 14 characters through the keyboard and after
the fo r loop the string is terminated by NULL character. Here, instead ofusinggets ( ) , getche () is
used. In get s () there may be a possibility that the entered text may be less than 15 characters. In such
a case some garbage values are also written in the file. To avoid the garbage we use getch e () within
the
fo r loop to exactly read characters equal to mentioned in thewri te () statement i.e. 15.
1336 Write * program to read text from a specified file from the disk. Use low-level disk
I/O operations.
# inc lude < s td io .h >
# include <conio.h>
# include <io.h>
# include < fcn tl.h>
# include <process.h>
void main()
{
char file [10] ,ch;
in t s;
c l r s c r ( ) ;
puts("n Enter a file name
getsifile);
s=open(file,0_RDONLY);
if ( s ~ - l )
I
puts("n File does not exits.");
exit(l);
)
else
{
while (!eof(s))
I
read(s,&ch,l);
putch(ch);
i
close(s);
)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.157.197