win32 与 C库 复制文件的比较

win32 与 C库 复制文件的比较
2009年10月18日
  
(1)C库复制(2)windows实现(3)windows函数调用
  以这三者的进行复制gogolepinyininstaller.exe(谷歌拼音输入法9.783 KB)进行测试.
  (1)cpC.c
  [b]#include
  #include
  #include
  #define BUF_SIZE 256
  int main (int argc, char *argv []) {
  clock_t start_t, finish_t ;
  FILE *in_file, *out_file ;
  char rec [BUF_SIZE] ;
  size_t bytes_in, bytes_out ;
  start_t = clock () ;
  if (argc != 3) {
  printf ("Usage: cpC file1 file2\n") ;
  return 1 ;
  }
  in_file = fopen (argv [1], "rb") ;
  if (in_file == NULL) {
  perror (argv [1]) ;
  return 2 ;
  }
  out_file = fopen (argv[2], "wb") ;
  if (out_file == NULL) {
  perror (argv [2]) ;
  return 3 ;
  }
  while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file)) > 0) {
  bytes_out = fwrite (rec, 1, bytes_in, out_file) ;
  if (bytes_out != bytes_in) {
  perror ("Fatal write error.") ;
  return 4 ;
  }
  }
  fclose (in_file) ;
  fclose (out_file) ;
  finish_t = clock () ;
  printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
  return 0 ;
  }
  (2)cpW.c
  [/b][b]#include
  #include
  #include
  #define BUF_SIZE 256
  int main (int argc, LPTSTR argv []) {
  clock_t start_t, finish_t ;
  HANDLE hIn, hOut ;
  DWORD nIn, nOut ;
  CHAR Buffer [BUF_SIZE] ;
  start_t = clock() ;
  if (argc != 3) {
  printf ("Usage: cpW file1 file2\n") ;
  return 1 ;
  }
  hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL) ;
  if (hIn == INVALID_HANDLE_VALUE) {
  printf ("Cannot open input file. Error: %x\n", GetLastError ()) ;
  return 2 ;
  }
  hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
  if (hOut == INVALID_HANDLE_VALUE) {
  printf ("Cannot open output file. Error: %x\n", GetLastError ()) ;
  return 3 ;
  }
  while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
  WriteFile (hOut, Buffer, nIn, &nOut, NULL) ;
  if (nIn != nOut) {
  printf ("Fatal write error: %x\n", GetLastError ()) ;
  return 4 ;
  }
  }
  CloseHandle (hIn) ;
  CloseHandle (hOut) ;
  finish_t = clock () ;
  printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
  return 0 ;
  }[/b]
  (3)cpF.c
  [b]#include
  #include
  #include
  int main (int argc, LPTSTR argv []) {
  clock_t start_t, finish_t ;
  start_t = clock () ;
  if (argc != 3) {
  printf ("Usage: cpF file1 file2\n") ;
  return 1 ;
  }
  if (!CopyFile (argv [1], argv [2], FALSE)) {
  printf ("CopyFile Error: %x\n", GetLastError ()) ;
  return 2 ;
  }
  finish_t = clock () ;
  printf ("%.2f seconds\n", (double)(finish_t - start_t) / CLOCKS_PER_SEC) ;
  return 0 ;
  }[/b]
  下面,输入比较的结果
  
  
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值