C语言实现复制文件

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

#define BUFSIZE 1024
//int a[100];
//void Process_bar(int LeftLen, int FileLen); this function is created to show the process bar
// but It not does well ,I decide design it in somedays
// hope some people could give more better suggestions, thanks!!!



int main(int argc, char *argv[])
{
/*int i;
for(i=0; i<100; i++)
{
a[i] = 0;
printf("=");
}*/ //using for Process_bar()----> Init the a[100];
if(argc!=3) //parameters for input is three copy file_src file_des
{
  printf("error : %s srcfile desfile \n",argv[0]);
  return -1;
}


FILE *fp_src, *fp_des; // define two pointer of file type fp -> src ; fp -> des
char ReadBuffer[BUFSIZE]; // the file stream buffer
int ReadLen = 0; // define the ReaddLen which the fun have read the file size
long FileLen = 0; // defile the FileLen means the size of the src file
int LeftLen; // define the LeftLen which the leave size of the file

fp_src = fopen(argv[1], "rb");
if(fp_src == NULL)
{
  printf("Open %s error !\n",argv[1]); //confirm the src file have been open right
  return -1;
}
fseek(fp_src, 0, SEEK_END); //you should kown the funtion of "fseek" and "ftell"
FileLen = ftell(fp_src);
fseek(fp_src, 0, SEEK_SET);

fp_des = fopen(argv[2], "ab");
LeftLen = FileLen;


while(1)
{
  if((LeftLen<=0) || (ReadLen >= FileLen)){
  break;
  }
  if(LeftLen >= BUFSIZE){
    memset(ReadBuffer, '\0', BUFSIZE);
    fread(ReadBuffer, BUFSIZE, 1, fp_src);
    write(ReadBuffer, BUFSIZE, 1, fp_des);
    ReadLen += BUFSIZE;
  }
  else{
    memset(ReadBuffer, '\0', BUFSIZE);
    fread(ReadBuffer, LeftLen, 1, fp_src);
    fwrite(ReadBuffer, LeftLen, 1, fp_des);
    ReadLen += LeftLen;
  }
  LeftLen = FileLen - ReadLen;
  system("cls");
  printf("========%2f%%=========\n",ReadLen*1.0/FileLen);
  _sleep(50);
  //Process_bar(LeftLen, FileLen);
}

fclose(fp_src);//Don't forget close the file ;
fclose(fp_des);
return 0;


}


/*void Process_bar(int LeftLen, int FileLen)
{
int ReadLen = FileLen - LeftLen;
float bit = ReadLen*1.0/FileLen;
int Bit = bit*100;
a[Bit] = 1;
int i;
system("cls");
for(i=0; i<100; i++)
{
if(a[i] == 0)
printf("=");
else
printf(">");
_sleep(10);
}
}*/

posted @ 2013-01-24 21:33  Auris  阅读(517)  评论(0编辑  收藏  举报