Dec 3, 2019

5 File Input/Output








There are certain application where input has to come from storage devices for the bulk of data to be processed is so large that it is impractical to re-enter data at process time or that hard copy of results may be required or the processed data be stored for later use.  In such cases non-standard devices like printers and disks or tapes is arranged in logical units by the operating system and is called as file.

File I/O : 
C allows two different types of files stream oriented or buffered I/O and system oriented or unbuffered I/O.

Stream oriented files are easy to deal with.  As C allows an interface between actual file and program.  This interface in the form of associating a file to a stream.  These files work with buffers and I/O is often termed as buffered I/O.  stream can be link to any file or device irrespective of its nature and be considered as file.

The system oriented or unbuffered I/O depends on operating system’s interpretation of the file.  There are no buffers maintained by C. I/O interface for read/write call the control is transferred to operating system.

Buffered I/O:  
These functions are part of standard library.  It maintains internal buffer to synchronize I/O calls.  With first read call, a big chunk is read and stored in this buffer.  All subsequent read requests are satisfied from this buffer.  Another read is done only on buffer gets empty.  Similarly all write requests are carried out in a buffer.  This buffer gets written to the disk when it is full or with an explicit call to flush out the buffer with buffered calls disk I/O gets optimized, but there is risk of data being lost on account of system crashes.

Buffered I/O functions used an internal structure to keep information regarding the file like its name, open mode, current position and internal buffer.  This structure is declared in stdio.h file and defined as FILE.  Function to open a file returns a pointer to this structure, required by all other functions accessing the file.  When a file is opened a stream or buffer is associated with this file.  Three streams are already opened by operating system are:

stdin           standard input device.
stdout         standard output device
stdprn         standard printer device.

Opening a File:
FILE *fopen (Char *filename, char *openmode).  This function opens a file with name given in filename.  Open mode specifies the mode specifies the mode in which the file has to be opened with the following values:



Mode Meaning
r open text file for reading.
w create the text file for writing.
a append / create to a text file.
rb open a binary file for reading
wb create a binary file for writing
ab append / create to a binary file
r+ open a text file for read / write.
w+ create a text file for read/write
a+ create a text file for read / write
rb+ open a binary file for read /write.
wb+ create a binary file for read write
ab+ append/ create a binary file.

fopen( ):
This function returns a pointer of type FILE if an error occurs while opening a file it returns a null.  After opening a file check should be done for errors. 
E.g.
#include<stdio.h>
FILE *fptr;
fptr = fopen (“Test” , “r”);
if(fptr == NULL)
{
printf(“error in opening Test”);
exist(1);
}

Null is defined in stdio.h exit( )function terminates the program and returns control to calling program.

Error checking in file I/O: 
On detection of an error the library function perror( ) can be used to print error message 
e.g.
void perror (char *user_message);
function first prints user message and then an error message to last file I/O call.

Controlling a File:  
There is a limit on number of files that can be opened at a time hence once a file has been used, it should be closed as it flushes out the buffers associated with the file.

int fclose(FILE *fileptr);
fileptr is stream associated with file and obtained by fopen( ) call, 
fclose( ) closes the file flushes out any any buffers and returns zero on success or –1 on error. 

E.g. 
fclose(fptr);

Determining End of File:  
The file input function return a value EOF on encountering an end of file, usually a check of return value is made to determine EOF.
e.g. if ((val = fgetc(fileptr))=eof)
          printf(“End of file encountered”);

feof( ): 
This function returns true on end of file otherwise returns false.
e.g.
int feof(FILW *fp);

Example
#include<stdio.h>
void main(int argc , char *argv[] ){
FILE *in, *out;
int ch;
if(argc<3){
printf(“Usage: Copy<file1><file2>”);
exit(1);
}
if (cin =fopen (argv[i], “r”)=NULL)
{
perror(argv[i]);
exit(1);
}
if (cout = fopen(argv[2], “w”))=NULL)
{
perror (argv[2]);
fclose(in);
exit(1);
}
while((ch = fgetc(in) !=-1){
fputc(ch,out);
}
fclose(in);
fclose(out);
}

Unbuffered I/O:  
Are direct calls to Operating System’s File I/O routines.  Unbuffered library calls do not have any buffer of their own.  These calls depend on operating system’s buffers.  For every R/W call the control transferred to operating system.  The fopen( ) call returns an integer, used as a handle to access the file in other calls.  All function returns –1 on error.
int open (char *file name, int mode)
mode can be
0 read only
1 write only
2 write/read
int create (char *file name, int mode)
int close (int handle)
int read (int handle, void *buffer, unsigned size)
int write(int handle, void *buffer, unsigned size)
int lseek(int handle, long offset, int from where)

Back

No comments:

Post a Comment