Thread: Simple C/P question in C++

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    11

    Simple C/P question in C++

    What code could I use to copy a file to another location on the computer?

    Thanks in advance,
    Will

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on what you want to achieve and on what OS, but the standard C way is to use two calls to fopen() to open the existing and create the new location file, and then fread()/fwrite() in a loop for as much as you need to, then fclose().

    On specific systems, such as Windows, there are system functions to do this for you, but it locks you in with that OS.

    --
    Mats
    Last edited by matsp; 02-09-2008 at 02:28 PM.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Thanks. That's all I need to know. If you wanted to know, I have windows xp pro. And I was just asking the question for future knowledge.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For Windows API, CopyFile and CopyFileEx works. There's also MoveFile and MoveFileEx.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Thanks Elysia, at my stage, every piece of info counts.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Depends on what you want to achieve and on what OS, but the standard C way is to use two calls to fopen() to open the existing and create the new location file, and then fread()/fwrite() in a loop for as much as you need to, then fclose().
    In C++, of course, one would use file streams. Perhaps ifstream, ofstream, and the >> and << operators. Maybe something like this (untested!):
    Code:
    #include <fstream>
    
    bool copy_file(const char *from, const char *to) {
        std::ifstream in(from);
        if(!in.is_open()) return false;
    
        std::ofstream out(to);
        if(!out.is_open()) return false;
    
        char c;
        while(in >> c) out << c;
    
        return true;
    }
    There's also MoveFile and MoveFileEx.
    There's also the standard C function rename() for that . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you kidding? That solution is horrible
    Reading and writing a char at a time will take forever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    Quote Originally Posted by Elysia View Post
    Are you kidding? That solution is horrible
    Reading and writing a char at a time will take forever.
    Thanks Elysia, you're a programming master! How the hell do you know everything?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're using the fstream classes you'd want to open the files in binary mode though, unless you know they're text files...
    Of course the easiest and least portable way to do it is using the system() function.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Thanks, all. And I'll keep this forum in my bookmarks! It's people are full of information.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The easiest way is this:

    Code:
    copy_to << copy_from.rdbuf();
    Where copy_from and copy_to are ifstream and ofstream, properly declared and opened. Using OS specific functions will be more optimal, however.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by PЯO View Post
    Thanks Elysia, you're a programming master! How the hell do you know everything?
    I don't believe Elysia knows everything, but it's pretty obvious that the overhead of checking and reading every single character of the input file and writing each one individually to the output file will be slower than doing it with blocks. fstream supports a read & write function [respectively for ifstream and ofstream, commonly for fstream]. Reading blocks of 4KB or so will be reasonably efficient (even multiples of 2 to the power of n is also a good idea, as disk blocks are always even powers of 2). Bigger blocks may work better in some OS's & HW combos, but not at all guaranteed [and the benefit gets smaller and smaller as the blocks grow bigger, until you start loosing out again for really big blocks].

    --
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    I don't believe Elysia knows everything...
    Hehehe well, from a newbie's perspective, we are all "gods" who know everything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Sorry for the inefficient program . . . my C++ skills are such that I didn't feel confident enough to post a solution that used blocks instead of characters . . . now ask for a C version, and you might get something.

    Hehehe well, from a newbie's perspective, we are all "gods" who know everything.
    I guess everyone is a newbie compared to Salem, then.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM