📁Beginner

Copy One File into Another File

File copy operation

Copy file contents from source to destination using character-by-character copy.

Program Code

file_copy.c
C
1#include <stdio.h>
2
3int main() {
4 FILE *source = fopen("source.txt", "r");
5 FILE *dest = fopen("copy.txt", "w");
6 char ch;
7
8 if (source == NULL || dest == NULL) {
9 printf("Error opening files!\n");
10 return 1;
11 }
12
13 // Copy character by character
14 while ((ch = fgetc(source)) != EOF) {
15 fputc(ch, dest);
16 }
17
18 fclose(source);
19 fclose(dest);
20
21 printf("File copied successfully!\n");
22 return 0;
23}
Output

File copied successfully!

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples