-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles_concept_flush.c
42 lines (32 loc) · 933 Bytes
/
files_concept_flush.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
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
FILE *fpr; // input file pointer
FILE *fpw; // output file pointer
char ch;
int ret;
if (argc != 3) {
printf("Usage: %s <input_file> <output_file>\n", argv[0]);
return 1;
}
fpr = fopen(argv[1], "r");
fpw = fopen(argv[2], "w");
if (fpr == NULL) {
printf("Error opening input file.\n");
return 1;
}
if (fpw == NULL) {
printf("Error opening output file.\n");
fclose(fpr); // Close the input file before returning
return 1;
}
while ((ch = fgetc(fpr)) != EOF) {
fputc(ch, fpw);
fflush(fpw); // Flush the output buffer to ensure immediate write
usleep(100000); // Delay in microseconds (100000 = 100 milliseconds)
}
printf("File copied successfully.\n");
fclose(fpr);
fclose(fpw);
return 0;
}