Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

C++ mv method

By: a guest on Feb 9th, 2013  |  syntax: C++  |  size: 0.76 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. void mv(std::string const &fname_from, std::string const &fname_to) {
  6.  
  7.     std::ifstream from(fname_from, std::ios_base::in | std::ios_base::binary); // open file for reading
  8.     if (!from) {
  9.         std::cerr << "Can't open " << fname_from << " for reading.\n";
  10.         throw 1;
  11.     }
  12.  
  13.     std::ofstream to(fname_to, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
  14.     if (!to) {
  15.         std::cerr << "Can't open " << fname_to << " for writing.\n";
  16.         throw 1;
  17.     }
  18.  
  19.     char c;
  20.     while (from.get(c))
  21.         if (!to.put(c)) {
  22.             std::cerr << "Error writing to " << fname_to << '\n';
  23.             throw 1;
  24.         }
  25. }
  26.  
  27. int main() {
  28.     mv ("a.exe", "b.exe");
  29. }