Error handling during I/O operations
While performing I/O operations on files errors may occur. For example reading
beyond EOF mark, trying to use a file that has not been opened or trying to
perform an operation on a file when the file has been opened for another type of
operation etc. If such errors are not monitored a program may behave abnormally
and may generate incorrect output. To check this we can make use of two status
inquiry library functions feof() and ferror().
The feof function can be used to test for the EOF 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 fp is
a pointer to a file that has been opened for reading then
if(feof(fp))
printf("End of data");
would display the message "End of data" on reaching the EOF condition.
The ferror function reports the status of the file indicated. It also takes a
file pointer as an argument and returns a nonzero integer if an error has been
detected upto that point during processing else it returns zero. The statement
if(ferror(fp))
printf("Error has occurred");
would print the error message if the reading is not successful.
We know that 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. For example
if(fp==null)
printf("File could not be opened");
Command line arguments
A command line argument is a parameter supplied to a program when the program is
invoked. In C, main is also a function, so just like any other function
arguments can be passed to main(). Here the only constraint is that the
arguments cannot be passed from any other function but should only be passed
from the command line i.e. the OS prompt. The arguments passed in this manner
are called command line arguments. 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 function main() is called by the system.
The first parameter argc is an argument counter that gives the count of the
arguments passed. The second parameter argv is an argument vector and represents
an array of character pointers that point to the command line arguments and the
size of this array will be equal to the value of argc. The declaration is as
follows:
main(argc,argv)
int argc;
char *argv<>;
{
………..
…………
}
compiled and linked the executable version of the file is produced which is
demo.exe. It is invoked from the OS prompt by typing the filename followed by
Enter. To pass arguments to the program from the command line they have to be
keyed in after the file name as shown below:
C:\>demo try1 try2 try3
Here argc = 4 and includes the program filename demo while the argument
vector contains the individual strings. argv<0> = C:\tc\demo.exe argv<1> = try1
argv<2>=try2 argv<3> = try3
Note: the first string in the argument vector argv<0> contains the complete
path of the executable version of the program file.
Let us illustrate this with a program to receive filename & line of
text as command line arguments & then writing the text to file.
#include
main(argc, argv) /* Main function with arguments */
int argc; /* Argument count */
char *argv<>; /* List of arguments */
{
FILE *fp;
int i;
char word<20>;
printf("\n Number of arguments in command line = %d",argc);
fp=fopen(argv<1>,"w"); /* Open file specified by argv<1> in the write
mode */
for(i=0;i
{
fprintf(fp,"%s",argv); /* Write to file argv<1> */
fputs(" ",fp); /* This adds space between the words of the text input
otherwise while
} reading from the file argv<1> all words will be considered as one string */
fclose(fp);
printf("\nContents of file %s :\n",argv<1>); /* Writes content of file
to screen */
fp=fopen(argv<1>,"r");
for(i=0;i
{
fscanf(fp,"%s",word);
printf("%s ",word);
}
fclose(fp);
for(i=0;i
{
printf("%s",argv);
}
Output:
C:\>demo try1 This is a test program
Number of arguments in command line = 7
Contents of file try1 :
This is a test program
C:\tc\demo.exe
try1
This
is
a
test
program