444 Programming and Data Structures
of line. In the same way ' t ' can be used to take the account of tabs. The text is also converted to
capital by using toupper () function and it is displayed.
b) f e of () The macro f eo f () is used for detecting the file pointer whether it is at the end of file or not.
It returns non-zero if the file pointer is at the end of file, otherwise it returns zero.
13.24 Write a program to detect the end of file using function feof(). Display the file pointer
position for detecting end of file.
# include <conio.h>
# include <stdio.h >
void main()
{
FILS *fp ;
char c;
fp -fo p e n ("t e x t.t x t" , mr " ) ;
c - f e o f ( f p ) ;
c l r s c r ( ) ;
printf ("File pointer at the beginning of the file : %dn",c);
while (Ifeofifp))
I
printf ("% c "^ );
C=getc(fp);
}
c=feof(fp);
printf ("File pointer at the end of file: %d ",c);
I
File pointer at the beginning of the file : 0
TECHNOCRATS LEAD THE WORLD
File pointer at the end of file : 32
Explanation At starting the f eof () function returns 0 (zero) because the file pointer is at the beginning
of file. After reading contents of file the file pointer reaches the end of file. At this stage the f e o f ()
function returns non-zero value with 32 in this program.
13.7 SEARCHING ERRORS IN READING/WRITING FILES
While performing read or write operations some times we do not get the result successfully. The
reason may be that the attempt of reading or writing the operation may not be correct. The provision
must be provided for searching the error while read/ write operations are carried out.
The 'C' language provides standard library function f e rr o r ( ) . This function is used for detecting
any error that might occur during read/write operation on a file. It returns a 'O' when the attempt is
successful otherwise non-zero in case of failure.
Files 445
c ) f error () The f error () is used to find out error when file read write operation is carried out.
13.25 Write- a program to detect an error while read /write operation of a file is in use.
# include <stdio.h >
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp ;
char n e x t »'Y ';
char name[2 5];
int marks;
flo a t p;
fp*fopen ("m arks.dat'*, " t" ) ;
i f (fp**NULL)
{
puts("Can not open f i l s * ) ;
eocit(l) ;
}
c l r s c r ( ) ;
while (n ert=='Y )
I
printf ( Enter Name, Marks, Percentage");
scanf ("% s %d %f',name,&marks);
p=marksl7;
fprintf(fp,"%s %d % f rname,&marks,&p);
if (ferroiifp))
I
printf ("n Unable to unite data ? ");
printf ("n File opening mode is incorrect.");
fclose(fp);
exitil);
I
printf ("Continue(Y/N");
fflush(stdin);
next=getche();
I
fclose(fp);
I
OUTPUT:
Enter Name, Marks
KAMAL 540
446 Programming and Data Structures
Unable to write data-?
File opening mode is incorrect.
Explanation In the above program the f p r in t f () function fails to write the entered data to the file.
Because it is opened in read mode. Hence, write operation can not be done. The error generated is
caught by the f e r r o r () function and the program terminates.
13.26 Write a program to catch the error that is occurred while read operation of a file using
ferror 0 function.
# include <stdio.h>
# include <process.h>
# include <conio.h>
void main()
{
FILE * f ;
char c;
c l r s c r ( ) ;
f=fopen("io8.c"r"w ");
if (f= N U L L )
I
printf(“ Cannot open file");
exitfl);
I
while ((c=fgetc(f))!=EOF)
I
if (ferrorif))
I
printf ( XnCan't read file.");
fclosetf);
exit(l);
i
printf ( “%c",c);
getch();
I
fcloseif);
I
OUTPUT:
Can't read file.
Explanation In the above program the file " i o 8 . c " is opened in write mode. Whereas the function
f g e tc () would certainly fail because it is used for reading operation or fg e tc () tries to read the
file. This never happens because the file that is opened in write mode cannot be read and vice-versa.
Files 447
rfjperror () It is a standard library function which prints the error messages specified by the compiler.
A program is illustrated below for understanding.
13.26 Write a program to detect and print the error message using perror 0 function.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
I
FILE *fr;
char c,file[]="lines.txt";
fr=fopen(file,"w");
clrscr();
while (!feof(fr))
{
c=fgetc(fr);
if (ferror(fr))
{
perror (file);
exit(l);
}
else
printf ("%c",c);
}
fclose(fr);
}
OUTPUT:
lines.txt: Permission Denied
Explanation In the above program to print the error message user can use p e rro r () function
instead of p r i n t f (). The output of the above program is "lines.txt: Permission Denied". We can also
specify our own message together with the system error message. In the above example ' f i l e ' variable
prints the file name " l i n e s . tx t " together with the compiler's message "Permission Denied".
e ) f t e l l 0 It is a file function. It returns the current position of the file pointer. It returns the
pointer from the beginning of file.
13.28 Write a program to print the current position of the file pointer in the file using fteUO
function.
# in clu de < s td io .h >
# include cconio.h>
448 Programming and Data Structures
void tnain()
{
FILE *fp;
char ch;
fpafcpsnf "text, tx t * ,* !* );
fseek<fp,21,SBBK_SET);
c h a fg e t c (fp )/
c lr s c r O ;
while dfeofffp))
{
printf ( %ct"fih);
printf r % d n "tftell(fp));
ch=fgetc(fp);
I
fcloseifp);
i
OUTPUT:
W 22
O 23
R 24
L 25
D26
28
Explanation In the above program fsee k () function sets the cursor position on byte 21. The
f g e tc () function in thewhile loop reads thecharacter after 21st character. The f t e l l () function
prints the current pointer position in the file. When f e o f () function is found at the end of file, the
program terminates.
f) rewind () This function resets the file pointer at the beginning of the file.
13.29 Write a program to show how rewindO function works.
# include <stdio.h>
# include cconio.h>
void main ()
{
FILE *fp ;
char c;
fp«fopen("text.txt","x" ) ;
clrscrO ;
fseek(fp,12,SEEK_SET);
printf ("Pointer is at %dn", ftell(fp));
printf ( “Before rewindO:");
..................Content has been hidden....................

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