Users Online
· Guests Online: 38
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Copy File into Another File
Here is source code of the C Program to copy a file into another file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Copy a File into Another File
*/
#include <stdio.h>
void main(int argc,char **argv)
{
FILE *fp1, *fp2;
char ch;
int pos;
if ((fp1 = fopen(argv[1],"r")) == NULL)
{
printf("\nFile cannot be opened");
return;
}
else
{
printf("\nFile opened for copy...\n ");
}
fp2 = fopen(argv[2], "w");
fseek(fp1, 0L, SEEK_END); // file pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // file pointer set at start
while (pos--)
{
ch = fgetc(fp1); // copying file character by character
fputc(ch, fp2);
}
fcloseall();
}
$ cc file1.c
$ a.out /tmp/vmlinux mylinux
File opened for copy...
$cmp /tmp/vmlinux mylinux
$ ls -l mylinux
-rw-rw-r--. 1 adi adi 3791744 Jul 27 19:57 mylinux
/*
* C Program to Copy a File into Another File
*/
#include <stdio.h>
void main(int argc,char **argv)
{
FILE *fp1, *fp2;
char ch;
int pos;
if ((fp1 = fopen(argv[1],"r")) == NULL)
{
printf("\nFile cannot be opened");
return;
}
else
{
printf("\nFile opened for copy...\n ");
}
fp2 = fopen(argv[2], "w");
fseek(fp1, 0L, SEEK_END); // file pointer at end of file
pos = ftell(fp1);
fseek(fp1, 0L, SEEK_SET); // file pointer set at start
while (pos--)
{
ch = fgetc(fp1); // copying file character by character
fputc(ch, fp2);
}
fcloseall();
}
$ cc file1.c
$ a.out /tmp/vmlinux mylinux
File opened for copy...
$cmp /tmp/vmlinux mylinux
$ ls -l mylinux
-rw-rw-r--. 1 adi adi 3791744 Jul 27 19:57 mylinux
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.