-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53.cpp
36 lines (28 loc) · 947 Bytes
/
53.cpp
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
/*C Program to copy contents of one file to another file*/
#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char ch;
// Open the source file in read mode
sourceFile = fopen("example.txt", "r");
if (sourceFile == NULL) {
printf("Error opening source file.\n");
return 1;
}
// Open the destination file in write mode
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL) {
printf("Error opening destination file.\n");
fclose(sourceFile); // Don't forget to close the source file before returning
return 1;
}
// Read each character from the source file and write it to the destination file
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
printf("File copied successfully.\n");
// Close both files
fclose(sourceFile);
fclose(destinationFile);
return 0;
}