-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50.c
More file actions
37 lines (27 loc) · 689 Bytes
/
50.c
File metadata and controls
37 lines (27 loc) · 689 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Program to copy a file and also count no of char, word and line.
#include <stdio.h>
int main() {
FILE *fp1, *fp2;
char ch;
int chars = 0, words = 0, lines = 0;
fp1 = fopen("source.txt", "r");
fp2 = fopen("copy.txt", "w");
if (fp1 == NULL) {
printf("File not found");
return 0;
}
while ((ch = fgetc(fp1)) != EOF) {
fputc(ch, fp2);
chars++;
if (ch == ' ' || ch == '\n')
words++;
if (ch == '\n')
lines++;
}
fclose(fp1);
fclose(fp2);
printf("Characters = %d\n", chars);
printf("Words = %d\n", words + 1);
printf("Lines = %d\n", lines);
return 0;
}