C program for copies one file contents to another file


Levels of difficulty: / perform operation:

C Program which copies one file contents to another file. Here we read the one file and copies the characters to another file which are existed in the disc. Here we read the file names from the command line.

C 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) {
		puts("Target file cannot be opened.");
		fclose(fs);
		exit(0);
	}
	while(1) {
		ch=fgetc(fs);
		if (ch==EOF)
		   break; else
		   fputc(ch,ft);
	}
	fclose(fs);
	fclose(ft);
	getch();
}