Advertisement
obernardovieira

Copy binary file

Apr 8th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. //NOT BY ME!
  2.  
  3. // Copy a file
  4. #include <fstream>      // std::ifstream, std::ofstream
  5.  
  6. int main () {
  7.     std::ifstream infile ("test.png",std::ifstream::binary);
  8.     std::ofstream outfile ("saida.png",std::ofstream::binary);
  9.  
  10.     // get size of file
  11.     infile.seekg (0,infile.end);
  12.     long size = infile.tellg();
  13.     infile.seekg (0);
  14.  
  15.     // allocate memory for file content
  16.     char* buffer = new char[size];
  17.  
  18.     // read content of infile
  19.     infile.read (buffer,size);
  20.  
  21.     // write to outfile
  22.     outfile.write (buffer,size);
  23.  
  24.     // release dynamically-allocated memory
  25.     delete[] buffer;
  26.  
  27.     outfile.close();
  28.     infile.close();
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement