Copying binary files, like ZIPs

This is a discussion on Copying binary files, like ZIPs within the C++ Programming forums, part of the General Programming Boards category; How do I copy things like ZIP or EXE or DLL files? You can't just load the file word by ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    Copying binary files, like ZIPs

    How do I copy things like ZIP or EXE or DLL files? You can't just load the file word by word then save it word by word, because it just makes the program crash. I need some way to copy files WITHOUT using system(), because that is too slow.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You o/s may provide a copyfile function. Alternatively open the file in binary mode, read it into a buffer, write the buffer to the destination, read the next bit into a buffer, etc...

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I tried

    in_file.open("c:\\test.zip", ios::bin);

    but it still crashes. Could you give me some example code?

    I have seen other people open binary files with fstream, and it works right for them. The problem right now, is it hangs whenever I load the file.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Ok, here's the short version -

    Code:
    #include <iostream> 
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	ifstream in("Your.zip",ios::binary);
    	if(!in)
    		cerr << "File open error\n";
    
    	ofstream out("Copy.zip",ios::binary);
    	if(!out)
    		cerr << "File open error\n";
    
    	out << in.rdbuf();
    	
    	return 0;
    }

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    oops, I was using ios::bin instead of ios::binary

    I'll try your source out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying Files
    By HLA91 in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2007, 03:24 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  4. Help with binary files
    By tuxinator in forum C Programming
    Replies: 3
    Last Post: 12-01-2005, 01:11 AM
  5. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:12 PM

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