文件复制工具,显示大小和复制率? [英] File copy tool, show size and copy rate?

查看:132
本文介绍了文件复制工具,显示大小和复制率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。这段代码是将一个目录复制到linux中的另一个目录。我添加

Hello. This code is copy file one directory to another in linux. And I add

if (from==NULL) perror ("Error opening file");
  else
  {
    fseek (from, 0, SEEK_END); 
    size=ftell (from);
    fclose (from);
    printf ("Size of myfile.txt: %ld bytes.\n",size);
  }



此部分打印复制的文件大小..但它不起作用。怎么了?以及如何解决它?以及如何确定复制率?


this section to print copied file size.. But it doesn''t working. What is the wrong? And how to fix it? And how to determine copy rate?

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

int main(int argc, char *argv[])
long getFileSize(const char *filename)
{
  long size;
  FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen(argv[1], "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen(argv[2], "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

  if (from==NULL) perror ("Error opening file");
  else
  {
    fseek (from, 0, SEEK_END); 
    size=ftell (from);
    fclose (from);
    printf ("Size of myfile=: %ld bytes.\n",size);
  }
  return 0;
}

推荐答案

要复制文件,切勿读/写单个字节,切勿使用 FSEEK 。这是毫无意义的,可能会使您的代码过于缓慢,因此测量复制速率将毫无用处。 :-)



打开两个文件,一个用于只读,另一个用于写入,在一个循环中,读取和写入大块 b>(注意最后一个块的大小,实际使用读取大小)。要计算复制速率,请计算读取和写入每个块的点的时间,并按时间划分块的大小。您需要一些机制来通知用户速率和进度,因此您可以使用一些回调函数。



-SA
To copy a file, never read/write a single byte, never use fseek. It''s pointless and could make your code prohibitively slow, so measuring of the copy rate would be useless. :-)

Open two files, one for read only, another for write only, in a cycle, read and write the content in big blocks (take care about the size of the very last block, use actually read size). To calculate copy rate, time the points where each block is read and written and divide the size of block by time. You will need some mechanism to notify the user on the rate and progress, so you could use some callback function.

—SA


在到达应确定其长度的部分之前,您正在关闭该文件。此时仍然具有非null值,但fseek和ftell无法正常工作。在关闭文件之前将代码向上移动。
You are closing the file before you reach the section that should determine its length. At that point from still has a non-null value, but fseek and ftell cannot work. Move your code higher up before you close the file.


这篇关于文件复制工具,显示大小和复制率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆