File Copying in C

We have already used the function fgetc( ) which reads characters from a file. Its counterpart is a function called fputc( ) which writes characters to a file. As a practical use of these character I/O functions we can copy the contents of one file into another, as demonstrated in the following program. This program takes the contents of a file and copies them into another file, character by character.

#include <stdio.h>
int main()
{
	FILE *fs, *ft;
	char ch;
	fs = fopen("sourcefile.txt", "r");
	if (fs == NULL)
	{
		puts("Cannot open source file");
		exit(0);
	}
	ft = fopen("targetfile.txt", "w");
	if (ft == NULL)
	{
		puts("Cannot open target file");
		fclose(fs);
		exit(0);
	}
	while (1)
	{
		ch = fgetc(fs);
		if (ch == EOF)
			break;
		else
			fputc(ch, ft);
	}
	fclose(fs);
	fclose(ft);
	getchar();
	return 0;
}