Thread: Is this function to copy a file ok?

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    10

    Is this function to copy a file ok?

    Hi all,

    I need to write a program that simply copies a file.
    The code works but I'm not sure if I'm handling correctly the eventual error fread() and I think that I could use a bigger value for bufsize but that's not an issue I guess.

    Code:
    int fcopy(FILE *fdfrom, FILE *fdto)
    {
        int bufsize, retval;
    
    
        retval = -1;
        for (bufsize = 0x4000; bufsize >= 128; bufsize >>= 1)
        {
            register char *buffer;
    
    
            buffer = malloc(bufsize);
            if(buffer != NULL)
            {
                while (TRUE)
                {
                    register int n;
    
    
                    n = fread(buffer, sizeof(char), bufsize, fdfrom);
                    if(!n)
                    {
                        if(ferror(fdfrom))
                        {
                            perror("Error:");
                        }
                        else    /*EOF is reached*/
                        {
                            retval = 0;
                            break;
                        }
                    }
    
    
                    if (fwrite(buffer, sizeof(char), (unsigned int) n, fdto) != n)
                    {
                        break;
                    }            
                }
    
                free(buffer);
                break;
            }
        }
    
        return retval;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Throw that garbage out and try writing the code yourself.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There is no need to malloc.
    There is no need to have a variable buffer size.

    char buff [BUFSIZ];
    And your inner loop will do just fine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    I used three buffers for this: input, temp, and output:
    Code:
    if (setvbuf(fd, NULL, (int)_IOFBF, BUFSIZE) != 0) {
      fputs("Can't create output buffer\n", stderr);
      exit(EXIT_FAILURE); }

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by Salem View Post
    char buff [BUFSIZ];
    Probably
    Code:
    static char buff[BUFSIZ];
    if its inside a copy function so it only gets allocated once.
    Last edited by christophergray; 04-21-2018 at 02:17 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Making it static serves no purpose(*) here. It's allocated once when the function is called, which is basically one instruction on most modern processors.

    Further, making it a static variable immediately makes the function non reentrant.

    (*) pedant exclusion clause.
    Unless you're working on a memory constrained machine with limited memory and/or stack space, in which case you have a load of compromises to make.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by Salem View Post
    Further, making it a static variable immediately makes the function non reentrant.
    Does that mean the following code may fail to work sometimes? Or do I have no worries because the temp buffer is not volatile?
    Code:
    #define BUFSIZE 4096
    static void copy(FILE *source, FILE *dest) {
      size_t bytes;
      static char temp[BUFSIZE]; // allocate once
    
      while ((bytes =
              fread(temp, sizeof(char), BUFSIZE, source)
              ) > 0)
        (void)fwrite(temp, sizeof(char), bytes, dest); }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Does that mean the following code may fail to work sometimes?
    Well if you try to call it in a multi-threaded program, without some higher level function providing an exclusive mutex, you'll lose every time.

    Not to mention that temp is permanently allocated for the life of the program, regardless of how many times (including zero) that you call the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by Salem View Post
    >Not to mention that temp is permanently allocated for the life of the program, regardless of how many times (including zero) that you call the function.
    Oh! I should have called it perm, then. My program does have some paths that do not call this function even once. I'm going through Prata's C Primer Plus, which doesn't cover parallel threads at all, but I have another text, Klemen's 21st Century C, which does in its next to last chapter. Sounds like I can forget about static storage duration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with C function to copy file
    By blixel in forum C Programming
    Replies: 6
    Last Post: 08-03-2017, 12:44 PM
  2. Replies: 9
    Last Post: 05-08-2011, 12:14 PM
  3. Replies: 2
    Last Post: 03-04-2010, 04:19 AM
  4. Replies: 2
    Last Post: 03-11-2009, 07:52 AM
  5. function to copy file from one folder to another
    By Harman in forum C Programming
    Replies: 7
    Last Post: 05-15-2005, 11:39 AM

Tags for this Thread