how to copy binary files using Unix API's

This is a discussion on how to copy binary files using Unix API's within the C Programming forums, part of the General Programming Boards category; Originally Posted by rohan_ak1 I have checked file sizes on fedora 8 for both source and destination directories.I checked every ...

  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
    Katy, Texas
    Posts
    2,309
    Maybe I'm missing something, but why are you not specifying the O_BINARY flag?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Well, there's obviously something different about the two sets of functions, or else there wouldn't be two sets.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #24
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Ah, I see. Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Page 2 of 2 FirstFirst 12
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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21