Low level file input and output in C does not perform any formatting or buffering of data whatsoever, transferring blocks of anonymous data instead by making use of the underlying operating system's capabilities.
Low level I/O makes use of a file handle or descriptor, which is just a non-negative integer, to uniquely identify a file instead of using a pointer to the FILE structure as in the case of stream I/O.
As in the case of stream I/O a number of standard files are opened automatically :-
The following table lists some of the more common low level I/O functions, whose prototypes are given in
open() | opens a disk file |
close() | closes a disk file |
read() | reads a buffer of data from disk |
write() | writes a buffer of data to disk |
The open function has the following prototype and returns -1 if the open operation fails.
int open ( char *filename, int oflag [, int pmode] ) ;
where filename is the name of the file to be opened, oflag specifies the type of operations that are to be allowed on the file, and pmode specifies how a file is to be created if it does not exist.
oflag may be any logical combination of the following constants which are just bit flags combined using the bitwise OR operator.
O_APPEND | appends to end of file |
O_BINARY | binary mode |
O_CREAT | creates a new file if it doesn't exist |
O_RDONLY | read only access |
O_RDWR | read write access |
O_TEXT | text mode |
O_TRUNC | truncates file to zero length |
O_WRONLY | write only access |
pmode is only used when O_CREAT is specified as part of oflag and may be one of the following values
This will actually set the read / write access permission of the file at the operating system level permanently unlike oflag which specifies read / write access just while your program uses the file.
The close() function has the following prototype
int close ( int handle ) ;
and closes the file associated with the specific handle.
The read() and write() functions have the following prototypes
int read( int handle, void *buffer, unsigned int count ) ; int write( int handle, void *buffer, unsigned int count ) ;
where handle refers to a specific file opened with open(), buffer is the storage location for the data ( of any type ) and count is the maximum number of bytes to be read in the case of read() or the maximum number of bytes written in the case of write(). The function returns the number of bytes actually read or written or -1 if an error occurred during the operation.
Example : Program to read the first 1000 characters from a file and copy them to another.
#include <io.h> #include <fcntl.h> #include <sys\stat.h> void main() { char buff[1000] ; int handle ; handle=open(" test.dat", O_BINARY|O_RDONLY, S_IREAD | S_IWRITE ); if ( handle == -1 ) return ; if ( read( handle, buff, 1000 ) == 1000 ) puts( "Read successful"); else { puts( Read failed" ) ; exit( 1 ); } close( handle ) ; handle = open("test.bak", O_BINARY|O_CREAT|O_WRONLY| O_TRUNC, S_IREAD | S_IWRITE ) ; if ( write( handle, buff, 1000 ) == 1000 ) puts( "Write successful") ; else { puts( "Write Failed") ; exit( 1 ) ; } close( handle ) ; }
Low level file I/O also provides a seek function lseek with the following prototype.
long _lseek( int handle, long offset, int origin );
_lseek uses the same origin etc. as fseek() but unlike fseek() returns the offset, in bytes, of the new file position from the beginning of the file or -1 if an error occurs.
vFor Example : Program to determine the size in bytes of a file.
#include <stdio.h> #include <io.h> #include <fcntl.h> #include <sys\stat.h> void main() { int handle ; long length ; char name[80] ; printf( “Enter file name : ” ) ; gets( name ) ; handle=open( name,O_BINARY| O_RDONLY, S_IREAD | S_IWRITE ); lseek( handle, 0L, SEEK_SET ) ; length = lseek( handle, 0L, SEEK_END ) ; close( handle ) ; printf( “The length of %s is %ld bytes \n”, name, length ) ; }
All Rights Reserved. © 2024 BookOfNetwork