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 47 48 49 50 51 52 53 54 55 56 57 58
| #include <stdio.h> #include <time.h> #include <string.h> #include <fcntl.h>
int mycopy(int way, int step) { time_t start_tm, stop_tm; FILE *srcf,*destf; char buff[4096]; int i, sfd,dfd; time(&start_tm); for (i=0; i<1024; i=i+step) { if(way==1) { sfd=open("in.txt",O_RDONLY,0644); dfd=open("out.txt",O_WRONLY|O_CREAT|O_TRUNC,0644); read(sfd,buff,step); write(dfd,buff, step); close(sfd); close(dfd); } else { srcf=fopen("in.txt","r"); destf=fopen("out.txt","w"); fread(buff,step*sizeof(char),1,srcf); fwrite(buff,step*sizeof(char),1,destf); close(srcf); close(destf); } } time(&stop_tm); printf("\ntime used for copy file by means of %s,%d byte per time: %ds", way?"read,write":"fread,fwrite",step,(int)(stop_tm-start_tm)); return 0; }
int main (int argc, char **argv) { if (mycopy (1,1)) return 1; if (mycopy (0,1)) return 1; if (mycopy (1,1024)) return 1; if (mycopy (0,1024)) return 1; return 0; }
|