You're here: Snippet Directory » C/C++ (495)
Language:

ISO source for copy in C++

Language: English
Programming Language: C++
Published by: rigelf [not registered]
Last Update: 5/15/2006
Views: 152


Description

Unlike many disk programs, which claim to be in C++ (and are actually in bad C), this is actually uses file io from C++. Just another simple function to copy a file. Possible improvements: change so it writes reads more than 1 byte at a time, add support for flags, etc. Should compile on any ANSI compliant compiler.

Code

1 #include <fstream> 2 #include <string> 3 using namespace std; 4 5 unsigned long copyfile(const string &from, 6 const string &to) 7 { 8 unsigned long ul_size(0); 9 10 if(from.size() && to.size()) 11 { 12 fstream file_from(from.c_str(), 13 ios::in | ios::binary); 14 fstream file_to(to.c_str(), 15 ios::out | ios::binary); 16 17 if(file_from.good() && file_to.good()) 18 { 19 file_to.tie(&file_from); 20 21 file_to << file_from.rdbuf(); 22 ul_size = file_to.tellp(); 23 24 file_from.close(); 25 file_to.close(); 26 } 27 } 28 29 return ul_size; 30 } 31 32 33 int main (int argc, char *argv[]) 34 { 35 if(argc>2) 36 { 37 copyfile(argv[1], argv[2]); 38 } 39 40 return 0; 41 } 42

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS