Bài viết mới

Tuesday, March 22, 2016

Funny code 1 - Copy file

Code C/C++ thực hiện copy file có định dạng bất kì từ một địa chỉ sang địa chỉ khá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
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <time.h>
const char source[]="E:\\test_inputfile.exe";
const char path[]="D:\\test_outputfile.exe";
#define oneread 10000000
char s[oneread];
long long value=0,cou,current=0,time_begin,i;
int main()
{
    FILE *f=fopen(source,"rb");
    FILE *f1=fopen(path,"wb");
    while ((cou=fread(s,1,oneread,f))!=0) value+=cou;
    fseek(f,0,0L);
    if (f==NULL || f1==NULL)
    {
        if (f==NULL) printf("Source file not found!!");
        if (f1==NULL) printf("Path directory not correct!!");
        return 0;
    }
    system("color 2");
    while ((cou=fread(s,1,oneread,f))!=0)
    {
        time_begin=clock();
        fwrite(s,1,cou,f1);
        current+=cou;
        system("cls");
        printf("Size of file: %.2f MB\n",value/1024.0/1024);
        for (i=1;i<=100*(value-current)/value;i++) printf("|");
        if (100*(value-current)/value==0) printf("\nComplete\n");
        else printf("\nCopying... %I64d%% remain\n",100*(value-current)/value);
        printf("Speed %.2f MB/s",cou/((clock()-time_begin)/1000.0)/1024/1024);
    }
    fclose(f);
    fclose(f1);
    return 0;
}

No comments:

Post a Comment