Results 1 to 8 of 8

Thread: Copy function?

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Copy function?

    Is there a copy function in C++? Currently I am using system("copy a b") but this prints out "x file(s) copied" which I don't want. Any ideas?
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Zaei
    Guest
    Write your own:
    Code:
    int copy(char* f1, char* f2)
    {
       FILE* fp1 = NULL;
       FILE* fp2 = NULL;
       fp1 = fopen(f1, "w+b");
       fp2 = fopen(f2, "wb");
       unsigned long flen;
       fseek(fp1, SEEK_END, 0);
       flen = ftell(fp1);
       char t;
       unsigned long i = 0;
       while(i <= flen)
       {
             fread(&t, 1, 1, fp1);
             fwrite(&t, 1, 1, fp2);
       }
       fclose(fp1);
       fclose(fp2);
    }
    Off the top of my head, but it should work. You could speed it up by grabbing perhaps 500k chuncks at a time, instead of a single byte, but Ill leave that up to you =).

    Z.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    WinAPI has CopyFile.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Zaei: Doesn't work, all I ended up with was a 3MB file
    CornedBee: I'd prefer not to use the API. I am trying to not go platform-dependent.
    Alcohol & calculus don't mix.
    Never drink & derive.

  5. #5
    Zaei
    Guest
    You should be able to fifure it out...

    Z.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The problem is that you're at the end of the file...
    Add a rewind(fp1); before reading data.
    And I'd open it in pure read mode (rb), not read/write.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Zaei
    Guest
    It was midnight... thats my only excuse =).

    Z.

  8. #8

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Ok, I tried writing this:
    Code:
    void copy(const char* srcpath, const char* destpath)
    {
        FILE* src = fopen(srcpath, "rb");
        FILE* dest = fopen(destpath, "wb");
        char t;
    
        while (!feof(src))
        {
            t = fgetc(src);
            fputc(t, dest);
        }
    
        fclose(src);
        fclose(dest);
    }
    Works fine, except it prints ÿ at the end of the new file. Where's it coming from and how can I fix it?
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width