2013年11月5日 星期二

[C] Copy the file to another by using C language

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 65536
int main(int argc, char *argv[])
{
    FILE *fp,*copy;
    char *buf;
    size_t ret;

    if(argc!=2)
    {
        printf("Just need one parameter\n");
        system("pause");
    }
    else
    {
        printf("copy now ...\n");
        fp=fopen(argc[argv-1],"rb");
        copy=fopen("copy","wb");
        buf = (char *)malloc(BUFFER_SIZE); //Dynamic allocate memory to Array
        ret = fread(buf, 1, BUFFER_SIZE, fp); // fread return the number of byte

        while(ret > 0)
        {
            fwrite(buf, 1, ret, copy);
            ret = fread(buf, 1, BUFFER_SIZE, fp);
        }

        free(buf);
        fclose(fp);
        fclose(copy);
    }

    return 0;

}


Reference :

Related Posts:

0 意見:

張貼留言