Equivalent of Dos 'copy' in C ?

This is a discussion on Equivalent of Dos 'copy' in C ? within the C Programming forums, part of the General Programming Boards category; Hi all, I've got another question. Is there a function in C for copy? I saw this somewhere and wondering ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    Smile Equivalent of Dos 'copy' in C ?

    Hi all,

    I've got another question. Is there a function in C for copy?
    I saw this somewhere and wondering if there's one for 'copy':

    remove("Filename.txt"); // equivalent to 'delete filename.txt in dos
    rename("Fileold.txt", "Filenew.txt"); // equivalent to 'rename fileold.txt filenew.txt

    what about .. copy file1.txt file2.txt .. is there one for C?

    I tried copy, copyfile, filecopy all not working.

    Thanks,
    YetBo

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No Copy function. You'll have to read and write the file. Or, you could use the system() function and pass the copy command (which will then read and write the file for you).
    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. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Hi Dino thanks for the quick reply.

    Mind showing me how the system() function works.

    And Happy Birthday !!!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Thanks!

    system("copy a b") ;
    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!

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The string passed to system is executed as though it was entered at the command line. This makes it nice & easy for some things - but whatever you put in there is OS-dependant, and not really part of C, per se. Sometimes it's viewed as bad style.

    That being said, copy still works in Windows, on *nix you'd use the 'cp' command.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Actually, switching out to the system copy command with system() is probably the most painless method.

    Copying a file is more than just creating a file with the same contents as some other file. You also need to preserve the creation date, file permissions, ownership, and any other attributes. Settings these attributes is a very non-portable operation, and in fact, it is impossible to implement a true file copy in C using only standard functions.

    So if you're going to delve into non-portability (and you must, if you need to copy files), you may as well reduce the pain to the minimum level possible by just calling system().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by brewbuck View Post
    Actually, switching out to the system copy command with system() is probably the most painless method.

    Copying a file is more than just creating a file with the same contents as some other file. You also need to preserve the creation date, file permissions, ownership, and any other attributes. Settings these attributes is a very non-portable operation, and in fact, it is impossible to implement a true file copy in C using only standard functions.

    So if you're going to delve into non-portability (and you must, if you need to copy files), you may as well reduce the pain to the minimum level possible by just calling system().
    Well, I think it can be done in a portable way through glib.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by MisterIO View Post
    Well, I think it can be done in a portable way through glib.
    What exactly do you think glib does internally? It calls platform-specific functions. There's nothing wrong with that, but it's incorrect to say that linking to some library which hides the non-portable details somehow makes your code portable.

    Your code still won't compile on any platform which isn't supported by glib. That's non-portable by definition.

    Non-portability is not a bad thing. The portable core of C and C++ is extremely restricted in what it can do. Real programs HAVE to go outside of those bounds.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by brewbuck View Post
    What exactly do you think glib does internally? It calls platform-specific functions. There's nothing wrong with that, but it's incorrect to say that linking to some library which hides the non-portable details somehow makes your code portable.

    Your code still won't compile on any platform which isn't supported by glib. That's non-portable by definition.

    Non-portability is not a bad thing. The portable core of C and C++ is extremely restricted in what it can do. Real programs HAVE to go outside of those bounds.
    No, your definition of portability is wrong. By your definition, not even the c standard library would be considered portable.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    No, your definition of portability is wrong.
    What exactly is your definition of portability, then? Because what he said falls in line with every other statement I've heard about portability? The C standard library is a uniform interface that is standardized across all interfaces - but the code to implement is extremely platform dependant. Working with files, etc...

    brewbuck - good point, I agree - I was sharing that more as an intro to the "system" function in general though. I think it's grossly overused by people who have recently discovered it.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by sean View Post
    What exactly is your definition of portability, then? Because what he said falls in line with every other statement I've heard about portability? The C standard library is a uniform interface that is standardized across all interfaces - but the code to implement is extremely platform dependant. Working with files, etc...

    brewbuck - good point, I agree - I was sharing that more as an intro to the "system" function in general though. I think it's grossly overused by people who have recently discovered it.
    I can simply use what he said about glib with the c standard library and prove that he's wrong:

    What exactly do you think "the c standard library" does internally? It calls platform-specific functions.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by MisterIO View Post
    I can simply use what he said about glib with the c standard library and prove that he's wrong:

    What exactly do you think "the c standard library" does internally? It calls platform-specific functions.
    The C library is a STANDARD which all compilers must provide in order to claim standards compliance. The features granted by the standard are required, and you can depend on them.

    glib is not a standard, is not required to be present on a system, is not required to be operable on any possible system.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by brewbuck View Post
    The C library is a STANDARD which all compilers must provide in order to claim standards compliance. The features granted by the standard are required, and you can depend on them.

    glib is not a standard, is not required to be present on a system, is not required to be operable on any possible system.
    That's completely unrelated to the concept of portability. Being standard and being portable are 2 completely different concept.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by MisterIO View Post
    That's completely unrelated to the concept of portability. Being standard and being portable are 2 completely different concept.
    If your code adheres to all standard C rules, then your code is portable to any environment with a standard C compiler. If you're claiming otherwise, I'd like to hear your argument.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by brewbuck View Post
    If your code adheres to all standard C rules, then your code is portable to any environment with a standard C compiler. If you're claiming otherwise, I'd like to hear your argument.
    Again, this is completely irrelevant to the previous discussion and I never said that. It just seems to me that you're unable to admit that you were wrong.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creepy DOS and BIOS functions (need help!!!)
    By James00 in forum C Programming
    Replies: 9
    Last Post: 05-05-2003, 12:40 AM
  2. how many old school DOS programmers are left?
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 02-01-2003, 04:14 PM
  3. File systems?? (Winxp -> DOS)
    By Shadow in forum Tech Board
    Replies: 4
    Last Post: 01-06-2003, 08:08 PM
  4. Quick ? on copy constructor
    By Traveller in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2002, 10:31 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM

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