Thread: copy?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    49

    copy?

    I asked about the mkdir command for dos, now I wanna know if it's possible to copy the file's. like the command copy in dos. If it is please fill me in on what it is.
    Thanks
    -Kavity
    i'm not stupid, just a little short on brains.
    Kav's game!
    Featuring:
    # - Goodguy mc goodguy
    * - Bad guy mc badguy
    $ - Princess Mc Cess
    @ - Mcdonalds Mc chicken! mmmmmm

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    With fopen you can open files for reading and/or writing.

    Open your source-file for reading and your destination-file for writing and use for example fgetc and fputc to get a byte from the source-file and to put it in the destination-file.

    After that you can use fclose to close both files.

    Perhaps this little piece of code may help.

    FILE *in, *out;
    unsigned char byte;
    in = fopen ("myinfile.dat", "rb");
    out = fopen ("myoutfile.dat", "rw");
    while (!feof (in))
    {
    byte = (unsigned char) fgetc (in);
    fputc ((int) byte, out);
    }
    fclose (in);
    fclose (out);

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    49

    Unhappy Hmm,

    Thanks, but I'd rather not have to use fopen. Is there any other way to do it?
    -Kavity
    i'm not stupid, just a little short on brains.
    Kav's game!
    Featuring:
    # - Goodguy mc goodguy
    * - Bad guy mc badguy
    $ - Princess Mc Cess
    @ - Mcdonalds Mc chicken! mmmmmm

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In forum C Programming, topic File reading and writing, there Prelude gives some alternatives to this. I suggest you take a look at those.

    Why won't you use fopen?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    49

    Won't use fopen?

    I don't really want to use fopen. The code is already almost written, it makes the directorys. And it's all done by using ofstream. And if I was to use fopen I would have to rewrite the code. I may do that later on, but for now I'm just wondering. Thanks anyways.
    -Kavity
    p.S.: I'll check that out.
    i'm not stupid, just a little short on brains.
    Kav's game!
    Featuring:
    # - Goodguy mc goodguy
    * - Bad guy mc badguy
    $ - Princess Mc Cess
    @ - Mcdonalds Mc chicken! mmmmmm

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    if you use the system(char * ) command, you can make any msdos calls you want to and call copy yourself.
    Code:
    system("copy file1.txt newfolder\\file1.txt");
    That would call the msdos system call copy, and copy file to newfolder.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Copying is quite simple really:
    Code:
    #include <fstream>
    
    using namespace std;
    
    void copy(const char* from, const char* to) {
        ifstream in(from);
        ofstream out(to);
        out << in.rdbuf();
        in.close();
        out.close();
    }
    
    int main() {
        copy("myfile1.txt", "myfile2.txt");
    }
    Of course it lacks error handling code, but generally that's all there is.
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM