What's Here?
- Members: 117,270
- Replies: 431,110
- Topics: 66,538
- Snippets: 2,391
- Tutorials: 630
- Total Online: 1,777
- Members: 69
- Guests: 1,708
Who's Online?
|
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!
|
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
|
|
|
Rating:
|
|
Views: 673 |
Language: C++
|
|
Last Modified: August 26, 2008 |
Snippet
/*
* A portable function to copy the contents of a file
* to another location
* author: Danny Battison
* contact: gabehabe@hotmail.com
*/
#include <fstream>
bool copyFile (const char SRC[], const char DEST[])
{
std::ifstream src; // the source file
std::ofstream dest; // the destination file
src.open (SRC, std::ios::binary); // open in binary to prevent jargon at the end of the buffer
dest.open (DEST, std::ios::binary); // same again, binary
if (!src.is_open() || !dest.is_open())
return false; // could not be copied
dest << src.rdbuf (); // copy the content
dest.close (); // close destination file
src.close (); // close source file
return true; // file copied successfully
}
/** EXAMPLE USAGE **/
#include <iostream>
int main ()
{
if (!copyFile ("C:\\lol.txt", "C:\\gabehabe.txt"))
std::cout << "File could not be copied successfully";
else
std::cout << "File copied successfully!";
std::cin.get (); // pause for input
return EXIT_SUCCESS; // program was executed successfully
}
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|