-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_file.c
43 lines (39 loc) · 940 Bytes
/
copy_file.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
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
void copyfile(char *source, char *destination){
FILE *ptr_old, *ptr_new;
ptr_old = fopen (source, "rb");
ptr_new = fopen (destination, "wb");
off_t file_size;
char *buffer;
struct stat s;
//int y = open (filename, O_RDONLY);
// fileno(ptr_old);
fstat(fileno(ptr_old),&s);
file_size = s.st_size;
buffer = (char*)malloc(file_size);
while(1){
int bytes_read = fread(buffer,sizeof(buffer),1,ptr_old);
if(bytes_read == 1){
fwrite(buffer,sizeof(buffer),1,ptr_new);
}else{
fwrite(buffer,sizeof(buffer),1,ptr_new);
break;
}
}
free(buffer);
fclose(ptr_new);
fclose(ptr_old);
}