Thread: fastest way to copy binary files...

  1. #1
    Unregistered
    Guest

    fastest way to copy binary files...

    would this be using ios::binary? since i will be copying *.exe so im wondering how it should be done... i dont want to use system since speed is an issue

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i think so. if speed is really an issue, you could use a FILE* pointer from C.

  3. #3
    Unregistered
    Guest
    Code:
    FILE *OriginalFile = fopen(Progname, "rb");
    FILE *CopiedFile = fopen(RndWords, "wb");
    int gk;
    while ((gk = fgetc(OriginalFile)) != EOF); 
    fputc(gk, CopiedFile);
    does this look right? and can anyone explain to what this does line for line?

  4. #4
    Unregistered
    Guest

    Unhappy

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	FILE *OriginalFile = fopen("C:\\Microsoft Visual Studio\\compile files\\Debug\\filecopy.exe", "rb");
    	FILE *CopiedFile = fopen("C:\\Microsoft Visual Studio\\compile files\\copyfile.exe", "wb");
    	int gk;
    	while ((gk = fgetc(OriginalFile)) != EOF); 
    	fputc(gk, CopiedFile);
    	return (0);
    }
    ok it copies the file but one file is 165kb big the orignal and the other is 1b big? what am i doing wrong?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A search of the boards would give you a very informative thread with quite a few different methods. Here, I'll give you a little boost:
    http://www.cprogramming.com/cboard/s...threadid=16157

    It's in C, but all of the methods can be converted to C++ fairly easily.

    -Prelude
    My best code is written with the delete key.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while ((gk = fgetc(OriginalFile)) != EOF);
    Remove the ;

    It means you do nothing until you reach the end of file, then you write a single char

    For speed, you need to copy a block at a time, not a char
    Eg.
    Code:
    #include <stdio.h>
    
    int main ( ) {
        char    buff[BUFSIZ];
        FILE    *in, *out;
        size_t  n;
    
        in = fopen( "a.exe", "rb" );
        out= fopen( "test.bin", "wb" );
        while ( (n=fread(buff,1,BUFSIZ,in)) != 0 ) {
            fwrite( buff, 1, n, out );
        }
        return 0;
    }

  7. #7
    Unregistered
    Guest
    thank you salem and prelude your great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help to copy files in c++
    By Helgso in forum C++ Programming
    Replies: 52
    Last Post: 11-21-2008, 08:50 AM
  2. Help with binary files
    By tuxinator in forum C Programming
    Replies: 3
    Last Post: 12-01-2005, 02:11 AM
  3. Binary files
    By Lionmane in forum C Programming
    Replies: 35
    Last Post: 08-25-2005, 01:56 PM
  4. Large Binary File Copy
    By Robert Austin in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 07:57 AM
  5. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 10:12 PM