440 Programming and Data Structures
# includ <proceas.h>
void mainO
{
FTLh fp;
atrucc bike
{
char nazzn [40];
int acvg;
float cost;
}»
struct bike a;
fp=*fopen("bk.txt*, "rh *);
i f tfptwiOTIJ.)
{
puts ("Cannot opaa f i l e * ) ;
cocitd) >
}
ClrecrO/
while (fread(&e,sizeof(e),lJp)Bzil)
printf ("n %s %d %2f'^.name^.avg^.cost);,
fcloseffp);
i
Q m m L
Model Name, Average, Prize: H O N D A 80 45OOO.0O
Model Name, Average, Prize: SUZUKI 65 43000.00
Model Name, Average, Prize: Y A M A H A 55 48000.00
Explanation In the above program the records are written in the binary mode on the disk in the file
"bk . tx t". Record writing will be over after the user presses 'N '.
The same file is opened in read mode. The frea d () function reads the records from the disk
which is to be placed in th e p rin tf () statement to display on the screen. The fre a d () function will
not read after detection of end of file. It returns 'O'.
Functions fread () & fw ri te () stores the numbers more efficiently and reading and writing of the
structures becomes easy.
13.6 OTHER FILE FUNCTION
6) The fseekf) function It is a file function. It positions file pointer on the stream. We can pass
three arguments through this function.
1. File Pointer.
2. Negative or positive long integer number to reposition the file pointer towards the backward or
forward direction.
3. The current position of file pointer.
Files 441
The Table 13.2 displays the various values of location of file pointer.
Table 13.2 Locations of file pointer
Integer Value Constant Location in the file
0
SEEK_SET Beginning of the file
1 SEEK_CUR Current position of the file pointer.
2 SEEK_END
End of the file
Example
fs ee k (£ p ,10,0) or fs e e k (fp ,10,SEEK_SET)
The file pointer is repositioned in the forward direction by 10 bytes.
13.21 Write a program to read the text file containing some sentence. Use fseek 0 and read
the text after skipping n characters from beginning of the file.
# inclu d e <stdio .h>
# inclu d e cconio.h >
v o id m&inO
{
FILE *£p;
in t n, ch;
c lr s c r O ;
fp=fopenCtext.txt", ”r");
printf ("nContents offilen");
while ((ch=fgetc(fp))!-EOF)
printf ("%c",ch);
printf ("nHow many characters including spaces would you like to skip ?
scanf ( “% d",bn);
fseek(fp,n,SEEK_SET);
printf ("n Information after %d bytesnn,n);
while ((ch=fgetc(fp))!=EOF)
printf ("%c",ch);
fcloseifp);
f
OUTPUT:
Contents of file:
THE C PROGRAMMING LANGU AGE INVENTED BY DENNIS RITICHE
How many characters including spaces would you like to skip ?: 18
Information after 15 bytes
1A N G U A G E INVENTED BY DENNIS RITICHE
442 Programming and Data Structures
Explanation In the above program w hile statement is used for checking the end of file. The file
pointer is initially positioned at the beginning of the file and the whole text is printed.
To reposition the file pointer the statement f seek () is used. The file pointer is to be positioned at
nth character from the beginning of the file and the characters from nth position onwards will be
printed on the screen.
In the above program one can use the statement f seek (fp, -n, SEEK_END) in place of
f seek (fp, n, SEEK_SET) to read from backward direction from the end of file.
13.22 Write a program to read last few characters of the file using fseekO statement
# include <dos.h>
# include <stdio.h>
# include <conio.h>
void m&inO
{
FILE *fp;
in t n,ch;
c l r s c r { ) ;
fp=fopen(text.txt","r")i
printf ("nContents offilen");
while ((ch*fgetc(fp))teEOF)
printf ("% c"^ h );
printf ( Hoxo many characters including spaces would you like to skip ?
scanf ('y 0d",&n);
fseek(fp^t-2rSEEK_END);
printf( Last %d characters ofaftlen",~l *n);
while ((ch=fgetc(fp))l=EOF)
printf ( u%c",ch);
fclose(fp):
)
Q U m i J ;
How many characters including spaces would you like to skip ?: 5
Last 5 characters of a file
WORLD
Explanation The statement f seek (fp ,n - 2, SEEK END) depositions the file pointer n-2 bytes in the
backward directions from the end of file. With this statement last characters of a statement can be
printed on the screen i.e. printing information will be from the position till the end of file. Here the
value of 'n ' entered is 5. To skip the account of NULL character and space decimal number 2 is
subtracted from n. Thus, we get the output WORLD.
13.23 Write a program to display V program files in current directory. The user should
select one of the files. Convert the file contents in capital and display the same on the
screen. Also calculate total characters and lines.
# include <ctype.h>
Files 443
# include <atdio.h>
# include cconio.h>
# include cprocess.h>
void mainO
{
PILE *fp ;
in t l>0 ,ca0,ch;
sta t ic ch$r f i l e [12]?
c lrs c r O j
systemC'dir *.clw");
printf ("n Enter a file name
scanf ("%s",file);
fp=fopen(file,"r”);
clrscrO;
printf ("nContents of 'c 'program File in capital case n ");
printf ("s:= s s = s = r = = = :s :s s s s :s s s u a s 3 s n sss a s a B s s s s s s & B s )| '')/'
while ((ch=fgetc(fp))!=EOF)
{
C++;
if (ch=='n')
1++;
printf C'%c",toupper(ch));
printf ("n Total Characters: %d",c);
printf ("n Total Lines : %d",l);
I
OUTPUT:
Enter a file name: IOIO.C
Contents of 'cr program File in capital case
MAINO
{
PRINTF (" HELLO WORLD");
}
Total Characters : 31
Total Lines : 4
Explanation In the above program system () statement is used for invokingMS. DOS command. The
d ir *. c/w displays contents of currents directory. The function f getc () reads the text of entered file
character by character till end of file. In the whi le loo p total characters and lines are counted. The line
termination is to be identified by ' n . Hence the i f statement is used for identification of termination
..................Content has been hidden....................

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