Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,270 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,777 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Copy a file from one location to another

A function which is not OS dependent, which copies one file to another by opening the file, and writing it to another. Works with all file extensions.

Submitted By: gabehabe
Actions:
Rating:
Views: 673

Language: C++

Last Modified: August 26, 2008

Snippet


  1. /*
  2. * A portable function to copy the contents of a file
  3. * to another location
  4. * author: Danny Battison
  5. * contact: gabehabe@hotmail.com
  6. */
  7.  
  8. #include <fstream>
  9.  
  10. bool copyFile (const char SRC[], const char DEST[])
  11. {
  12.     std::ifstream src; // the source file
  13.     std::ofstream dest; // the destination file
  14.  
  15.     src.open (SRC, std::ios::binary); // open in binary to prevent jargon at the end of the buffer
  16.     dest.open (DEST, std::ios::binary); // same again, binary
  17.     if (!src.is_open() || !dest.is_open())
  18.         return false; // could not be copied
  19.  
  20.     dest << src.rdbuf (); // copy the content
  21.     dest.close (); // close destination file
  22.     src.close (); // close source file
  23.  
  24.     return true; // file copied successfully
  25. }
  26.  
  27. /** EXAMPLE USAGE **/
  28.  
  29. #include <iostream>
  30.  
  31. int main ()
  32. {
  33.     if (!copyFile ("C:\\lol.txt", "C:\\gabehabe.txt"))
  34.         std::cout << "File could not be copied successfully";
  35.  
  36.     else
  37.         std::cout << "File copied successfully!";
  38.  
  39.     std::cin.get (); // pause for input
  40.     return EXIT_SUCCESS; // program was executed successfully
  41. }
  42.  

Copy & Paste


Comments


malvin 2008-08-20 06:51:10

I tried to use this but it doesn't work. It says "Type qualifier "std" must be a struct or a class name".

gabehabe 2008-08-20 07:39:10

That doesn't sound like an error caused specifically by my snippet. Everything prefixed with std:: is a part of the std namespace. Try removing those std:: prefixes, and then put "using namespace std;" at the beginning of the program. =)

malvin 2008-08-21 02:58:53

I put "using namespace std;" at the beginning of the program but it gives me this: "Declaration syntax error". :(

gabehabe 2008-08-24 08:12:20

Modified to open the files in binary, to prevent outputting jargon on an additional line. =)


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month