Files 449
while dfeofifp))
{
c=fgetc(fp);
printf ("% c",c);
I
printf ("bAfter rewindO
rewind(fp);
while (!feof(fp))
{
c=fgetc(fp);
printf <"%c"rc);
I
OUTPUT;
Pointer is at 12
Before rewindO: LEAD THE WORLD
After rewindO : TECHNOCRATS LEAD THE WORLD
Explanation In the above program f seek ( ) function sets the pointer position on 12 character. The
first while loop reads and prints the file from the current position up to the end in which first 12
characters of file are not displayed. Before starting of second while loop the rewind () function sets
the pointer position at the beginning of file i.e. on 1 character. The while loop reads all the character of
the file from starting to end. In short the rewind () function sets the file pointer at the beginning of the
file.
g ) UNLINK () OR REMOVE () These functions delete the given file in the directory. It is similar to del
command in dos.
13.30 Write a program to delete the given file from the disk using removeO or unlinkO
function.
# include <dos.h>
# include <stdio.h>
# include <canio.h>
# include <process.h>
void main()
{
FILE *fp ;
char f i l e [15];
c l r s c r ( ) ;
systemCdir *.txt");
printf ( " Enter The File Name
450 Programming and Data Structures
scanf ("%s",file);
fp=fopen(file/'r");
if (fp==NULL)
I
printf (" File does not exist");
exit(l);
)
else
I
remove(file);
printf ("n File ( %s) has been deleted !",file);
)
OUTPUT:
Enter The File Name : TEXT.TXT
File (TEXT.TXT) has been deleted !
Explanation In the above program file to be deleted is entered. The entered file is opened in read
mode. If the fo p en () fails to open the file then it returns NULL to pointer * fp i.e. file does not
exist and the program terminates. By using unlink () or remove () function the entered file is
deleted.
h) RENAME () This function changes the name of the given file. It is similar to dos command
rename.
13.31 Write a program to change the name of the file. Use renameO function.
# include <stdio.h>
# include cconio.h>
mainO
{
char o ld [1 2 ], new[1 2 ];
system ("dir * .c /w *);
printf ("nO ld File Nam e: “);
scanf ("% s",old );
printf ("N e w File Name : “);
scanf ("% s",n ew);
rename(old,new);
systemC'dir *.clw");
)
OUTPUT:
Old File Name : old.c
New File Name : new.c
Files 451
Explanation In the above program the rename () function changes the name of the given file with
new name. It's meaning is similar to dos command 1 rename' .
13.32 Write a program to copy the contents of one file to another file.
/* F ile copy program */
# include "std io .h "
# include "process.h"
void main()
{
/* Declaration * /
FILE * f t , * f s ;
in t c»0;
c lr s c r ( ) ;
/* Opening Files */
fs=fopen ("a. txt", "r");
ft=fopen ("b.txt","w ");
if (f s ~ N U L L )
I
printf ( Source file opening error.");
exit(l);
I
else
if(ft==NU LL)
printf ("n Target file opening error.");
exit(l);
}
I* Reading file */
while (Jfeofifs))
1
fputc(fgetc(fs),ft);
C + + ;
I
printf ("n %d Bytes copied from 'a.txf to ‘b.txt.’",c);
/* Closing Files */
c=fcloseall();
printf ( %dfiles closed.",c);
}
OUTPUT:
45 Bytes copied from 'a.txt' to 'b.txt.
2 files closed.
452 Programming and Data Structures
Explanation In the above program two files are opened. The source file " a . txt" is opened in read
mode and the target file " b . txt" is opened in write mode. With f putc () function the contents read by
f ge tc () is written to target file. To count the number of characters of source file c counter is used. The
f c lo se a ll () function closes all the opened files and returns number of files closed. This returns the
value of f clo se all () which is collected by variable c.
13.33 Read the contents of three files and find the largest file.
# include <stdio.h >
# include <conio.h>
# include < alloc .h >
# include <process.h>
void mainO
{
FILE * f [3 ],* fp »
in t x [ 3 ] , l , y * 0 #k *0 ,t«0 ;
char c l;
char name[ 3 ] [1 1 1 »{*1 . t x t * , "2 . t x t * , " 3 . t x t " } ;
c lr s c r O ;
for (l=0;l<3;l++)
I
fp-fopen(name[l],"r");
M = fp ;
if(fp== N U LL)
{
printf C'n %s file not found. “,name[l]);
exit(l);
/
clrscrO;
for (l=0;l<3;l++)
(
while (IfeofifllD)
I
cl=fgetc(fll]);
x[l]=y++;
I
y=0;
clrscrO;
fcloseallO;
for (l=0;l<=2;l++)
Files 453
printf ("F ile: %s Bytes: % d ",name [I]jc[l]);
t=t+x[l];
for (l= t;l>=l;l~ )
I
for (k=0;k<3;k++)
I
if(l= = x [k ])
printf ("% s are the largest file.",name[k]);
exit(l);
I
I
OUTPUT:
File:l.txt Bytes: 16
File: 2.txt Bytes:20
File : 3.txt Bytes : 25
3.txt are the largest file.
Explanation In the above program an array of file pointer and a separate file pointer f p are declared.
The two-dimensional character array is used for storing file names. Using file pointer array in the
f o r loop three files are opened in read mode. Each element of file pointer array points to the
corresponding elements of character array i.e. file names.
The f g e t c () function in the w h ile loop reads the contents of each file one by one. The numbers of
characters are also written to integer array in the same loop. At the end the values stored in integer
array are compared with one another and the file containing the largest number of bytes is detected.
The name of the largest file is displayed.
13.34 Write a program to copy up to 100 characters from a file to an array. Then copy the
contents of an array to another file.
# include <stdio.h>
# include <conio.h>
# include <process.h>
# define SIZE 100
void mainO
{
PILE * f , *f2;
static char c,ch[SIZE];
..................Content has been hidden....................

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