Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. This code is copy file one directory to another in linux. And I add
C#
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?
C#
#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;
}
Posted
Updated 2-May-13 4:20am
v2

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
 
Share this answer
 
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.
 
Share this answer
 
Comments
Purevochir 2-May-13 12:57pm    
I moved code in higher place and then it works but the program is copying just first one single byte.. What should I do?
nv3 2-May-13 13:28pm    
Of course, after positioning at the end and using ftell, you need to reposition the file before you start the copy loop!

Also, I recommend to follow the advice in Solution 1 and copy bigger chunks of data than just a single byte. This will improve the speed of the copying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900