-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyfile.c
46 lines (35 loc) · 1.08 KB
/
copyfile.c
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
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *sourceFile, *destinationFile;
char sourceFileName[100], destinationFileName[100];
char ch;
// Input the source file name
printf("Enter the source file name: ");
scanf("%s", sourceFileName);
// Open the source file in read mode
sourceFile = fopen(sourceFileName, "r");
if (sourceFile == NULL) {
perror("Error opening source file");
return 1;
}
// Input the destination file name
printf("Enter the destination file name: ");
scanf("%s", destinationFileName);
// Open the destination file in write mode
destinationFile = fopen(destinationFileName, "w");
if (destinationFile == NULL) {
perror("Error opening destination file");
fclose(sourceFile);
return 1;
}
// Copy contents from source to destination
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
// Close both files
fclose(sourceFile);
fclose(destinationFile);
printf("File copied successfully.\n");
return 0;
}