Copying Files

This is a discussion on Copying Files within the C++ Programming forums, part of the General Programming Boards category; Hi all I was wondering how could I make a console program that when run would copy files from a ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question Copying Files

    Hi all

    I was wondering how could I make a console program that when run would copy files from a set location to its location so for example if it was on a usb pen it would copy the files to the usb drive? I am a noob to C++ and I googled it but nothing came up that I could understand, could someone please point me in the right direction?

    Many thanks

    HLA91

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whilst the process of getting there is slightly different, the API function is the same: Copy files across a network

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

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The trick is finding out where your program lives. You can do that using GetModuleHandle() and GetModuleFilePath() (I think).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    I read the article but I am still a little confused can someone please give me a example program please?(like i said im a total noob only 4 days with C++)

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you just want to copy files to where your executable is located, just specify no path whatsoever (or "." for the path). This uses the current directory, which will be the location of your executable unless you've gone and messed with a shortcut's working directory or something.

    So, if you wanted to copy c:\file to the current directory, it's quite simple.
    Code:
    system("copy c:\file .");
    Not that I'm recommending you use system(), but the idea is the same for opening your file manually.
    Code:
    copy_file("c:\file", ".\file");
    where copy_file() is your own function.

    [edit] Without error checking, using text functions:
    Code:
    bool copy_file(const char *fromname, const char *toname) {
        std::ifstream from(fromname);
        if(!from.is_open()) return false;
    
        std::ofstream to(toname);
        if(!to.is_open()) {
            from.close();
            return false;
        }
    
        unsigned char c;
        while(from >> c) to << c;
    
        // not strictly necessary
        from.close();
        to.close();
    
        return true;
    }
    I think that would work, but I'm not sure. I could give you a C example, no problem. [/edit]
    Last edited by dwks; 10-24-2007 at 11:47 AM.
    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.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Only problem is that it's very inefficient since it only reads one char at a time :P
    There's also platform-specific API. In Windows, it's called CopyFile.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    . is the working directory. Not necessarily the directory the program is in.

    Also, don't forget to escape backslashes in strings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . is the working directory. Not necessarily the directory the program is in.
    I mentioned that. But an ordinary executable run from windows will have its current working directory set to the executable's location.

    Also, don't forget to escape backslashes in strings.
    Whoops.

    Only problem is that it's very inefficient since it only reads one char at a time :P
    Well, yes, that's true. You could copy buffers if you wanted to, but I didn't want to make the code too complicated.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Ok thanks guys I have used the
    system("copy c:\file .");
    that DWK mentioned that is fine for now and as I get better I can improve it

    Cheers

    HLA91

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using mmap for copying large files
    By rohan_ak1 in forum C Programming
    Replies: 6
    Last Post: 05-13-2008, 08:12 AM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  4. Copying binary files, like ZIPs
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 03-16-2002, 02:54 PM
  5. copying files
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2002, 02:41 AM

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