Task: Open a source and destination text file; copy character by character. While copying, count lines, words, and characters and print the summary.
#include <stdio.h>
#include <ctype.h>
int main(void){
char src[256], dst[256];
printf("Source file: "); scanf("%255s", src);
printf("Destination file: "); scanf("%255s", dst);
FILE *in = fopen(src, "r");
if(!in){ printf("Cannot open source.\n"); return 0; }
FILE *out = fopen(dst, "w");
if(!out){ printf("Cannot open destination.\n"); fclose(in); return 0; }
int ch, chars=0, words=0, lines=0, in_word=0;
while((ch=fgetc(in))!=EOF){
fputc(ch, out);
++chars;
if(ch=='\n') ++lines;
if(isalnum(ch)){ if(!in_word){ in_word=1; ++words; } }
else{ in_word=0; }
}
fclose(in); fclose(out);
printf("Copied. Lines=%d Words=%d Characters=%d\n", lines, words, chars);
return 0;
}