Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Linux copy-paste
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;
}
Posted 2-May-13 5:06am
Edited 2-May-13 5:20am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
 
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Purevochir at 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 at 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)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 Maciej Los 270
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 202
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 2 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100