Thread: how to copy binary files using Unix API's

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rohan_ak1 View Post
    I have checked file sizes on fedora 8 for both source and destination directories.I checked every file-size and it is the same.But the total file size is 0.1MB less.
    Is it possible that on one side, the directories take up some space, and the other side it doesn't, which accounts for the difference. If all the files have exactly (to the last digit) same size, then the files ARE the same size.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Maybe I'm missing something, but why are you not specifying the O_BINARY flag?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    31
    Thanks for all the help

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    31
    I tried using O_BINARY flag but it gives an error.I guess open() opens a file directly in binary mode.

    These are my open() calls for the source and destination :
    Code:
    fps=open(source,O_RDONLY);
    fpd=open(dest,O_WRONLY|O_CREAT,0777);

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no such thing as O_BINARY in Linux, as far as I can tell. It should certainly not make any difference, as line-endings in Linux are the same in C and in the actual files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I see, now that I've pulled out my Advanced Programming in the Unix Environment. (I was reading my Encyclopedia C, circa '91, that has a strong bent towards DOS).

    APUE says to use fread() and fwrite() for binary IO.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    I see, now that I've pulled out my Advanced Programming in the Unix Environment. (I was reading my Encyclopedia C, circa '91, that has a strong bent towards DOS).

    APUE says to use fread() and fwrite() for binary IO.

    Todd
    Yes, fread/fwrite is more "Nice" to use, but for direct file-copies, it serves little purpose and won't actually help much .

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, there's obviously something different about the two sets of functions, or else there wouldn't be two sets.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #24
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Todd Burch View Post
    Well, there's obviously something different about the two sets of functions, or else there wouldn't be two sets.
    read() and write() are operating system API calls, in other words the most basic level of I/O you can do on UNIX. fread() and fwrite() are standard functions which in all likelihood are built on top of read() and write().

    You're looking at two layers, not two totally different sets of functions.

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This is the code I have written for copying files from source to destination:
    Great, and right after I told you to use the return results, you post something which ignores them.
    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.

  11. #26
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Ah, I see. Thanks.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge Binary Files
    By chinook86 in forum C Programming
    Replies: 7
    Last Post: 01-21-2008, 02:19 PM
  2. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  3. Using Configuration Files with C on UNIX
    By emaxham in forum C Programming
    Replies: 1
    Last Post: 10-16-2001, 02:28 PM
  4. Binary files in C++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2001, 04:48 PM
  5. C function to copy files?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-11-2001, 01:10 AM