ALL SIR NOTES HAVE BEEN UPLOADED HERE.
A computer system stores programs and data in secondary storage in the form of files. Storing programs and data permanently in main memory is not preferred due to the following reasons:
Main memory is usually too small to permanently store all the needed programs and data.
Main memory is a volatile storage device, which loses its contents when power is turned off.
It is therefore necessary to have a more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data. A file is a place on the disk where a group of related data is stored.
File handling enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file:
Creation of the new file
Opening an existing file
Reading from the file
Writing to the file
Deleting the file
To perform file operations in C, the important file handling functions that are available in the C library are:
Data structure of a file is defined as FILE in the library of standard I/O function definitions. When we open a file, we must specify what we want to do with the file. For example, we may write data to the file or read the already existing data. Following is the general format for declaring and opening a file:
FILE *fptr;
fptr = fopen (“filename”, “mode”);
The first statement declares the variable fptr as a ‘pointer to the data type FILE’. The
second statement specifies the purpose of opening this file. Mode can be one the
following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding) data to it.
A file must be closed as soon as all operations on it have been completed. This ensures that all outstanding information associated with the file is flushed out from the buffers and all links to the file are broken. It also prevents any accidental misuse of the file. The I/O library supports a function to do this. It takes the following form:
fclose (file_pointer);
This would close the file associated with the FILE pointer file_pointer.
The getc and putc functions
The simplest file I/O function are getc and putc. These functions handle one character at a time.
putc ( ch, fptr );
writes the character contained in the character variable ch to the file associated with FILE pointer fptr.
ch = getc ( fptr );
would read a character from the file whose file pointer is fptr.
The getw and putw Functions
The getw and putw are integer-oriented function. They are used to read and write
integer values. The general forms of getw and putw are as follows:
putw ( int_num , fptr );
int_num = getw ( fptr );
The fprintf and fscanf Functions
The functions fprintf and fscanf perform I/O operations that are identical to the familiar printf anf scanf functions, except that they work on files. The first argument of these functions is a file pointer which specifies the file to be used. General form of fprintf is:
fprintf ( fptr, “control string” , list);
where fptr is a file pointer associated with a file that has been opened for writing. The control string contains output specifications for the items in the list.
The general format of fscanf is:
fscanf ( fptr, “control string”, list);
This statement would cause the reading of the items in the list from the file specified by fptr, according to the specifications contained in the control string.
It is possible that an error may occur during I/O operations on a file. Typical error situations include the following:
Trying to read beyond the end-of-file mark.
Device overflow.
Trying to use a file that has not been opened.
Trying to perform an operation on a file, when the file is opened for another type of operation.
Opening a file with an invalid filename.
Attempting to write to a write-protected file.
We have two status-inquiry library function, feof and ferror that can help us detect I/O errors in the files.
The feof function can be used to test for an end of file condition. It takes a FILE pointer as its only argument and returns a nonzero integer value if all of the data from the specified file has been read, and returns zero otherwise.
if ( feof ( fptr ) )
printf(“End of file”);
The ferror function reports the status of the file indicated. It also takes a FILE pointer as its argument and returns a nonzero integer if an error has been detected up to that point, during processing. It returns zero otherwise.
if ( ferror( fptr ) != 0 )
printf (“Error occurred”);
Whenever a file is opened using fopen function, a file pointer is returned. If the file cannot be opened for some reason, then the function returns a NULL pointer. This facility can be used to test whether a file has been opened or not.
if ( fptr == NULL )
printf (“File opening Error”);
There are occasions, however, when we are interested in accessing only a particular part of a file and not in reading the other parts. This can be achieved with the help of the functions fseek, ftell, and rewind available in the I/O library.
ftell takes a file pointer and return a number of type long, that corresponds to the current position. It takes the following form:
pos = ftell ( fptr );
rewind takes a file pointer and resets the position to the start of the file.
rewind ( fptr );
fseek function is used to move the file position to a desired location within the file. It takes the following form:
fseek ( fptr, offset, position );
Here fptr is a pointer to the file concerned, offset specifies the number of positions (bytes) to be moved from the location specified by position. The position can take one of the following three values:
0 for beginning of file
1 for current position
2 for end of file
The arguments passed from command line are called command line arguments. These arguments are handled by main() function. Command line argument is a parameter supplied to a program when the program is invoked. This parameter can be of any type.
In fact main can take two arguments called argc and argv and the information contained in the command line is passed on to the program through these arguments, when main is called up by the system.
The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument vector and represents an array of character pointers that point to the command line arguments. In order to access the command line arguments, we must declare the main function and its parameters as follows:
main ( int argc, char *argv[ ] )
{
.....
.....
}
The first parameter in the command line is always the program name and therefore argv[0] always represents the program name.