Description:
In this program we have to use the
file functions to perform the copy operation from one file to another file.
Algorithm:
Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments =3
or not. If not print invalid no of arguments
Step 4: open source file in read
mode
Step 5: if NULL pointer, then print source file can not
be open
Step 6: open destination file in
write mode
Step 7: if NULL pointer, then print
destination file can not be open
Step 8 : read a character from
source file and write to destination file until EOF
Step 9: Close source file and
destination file
Step 10: Stop
Program:
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(int argc, char *argv[])
{
FILE *fs,*ft;
char ch;
clrscr();
if(argc!=3)
{
puts("Invalid number of
arguments.");
exit(0);
}
fs =
fopen(argv[1],"r");
if(fs==NULL)
{
puts("Source file cannot be
opened.");
exit(0);
}
ft =
fopen(argv[2],"w");
if (ft==NULL) // check the condition if the file pointer is
NULL or not
{
puts("Target file cannot be
opened.");
fclose(fs);
exit(0);
}
while(1)
{
ch=fgetc(fs);
if (ch==EOF) // check the condition if the
file is end or not
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
getch();
}
Output:
source.c
this is
source text
ouput.c
Command line arguments
source.c ouput.c
source.c
this is
source text
ouput.c
this is
source text
Command line arguments
source.c
Invalid number of arguments.
Conclusion:
the program is error free
VIVA QUESATIONS:
1) What is file ?
Ans: The collection of alphabets is called file
2) What are the various operations performed on the file ?
Ans: fopen(), fread(), fwrite(), fclose() etc..,
3) What is the use of file pointer ?
Ans: The file pointer must be used in subsequent operations
on the file
No comments:
Post a Comment