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: 142


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 /* Idea taken from original code by rigelf*/ 2 /* Re-coded 12/18/00 by xbud - boredom *shrug*/ 3 /* If used ./pname src dst | wc -c 4 * it counts the periods! =) yay! hehe' */ 5 6 #include <fstream.h> 7 #include <string.h> 8 9 void usage ( char * ); 10 //It's a simple program if you can't figure it out you shouldn't 11 //be trying to read code! 12 13 int main (int argc, char *argv[]) { 14 fstream ifile, ofile; 15 16 char buff[1]; 17 18 if( argc < 3 ) { 19 usage(argv[0]); 20 } 21 if( argv[1] == argv[2] ) { 22 cerr << "Can't Copy file to itself!\n" << endl; 23 return -1; 24 } 25 26 ifile.open( argv[1],ios::in ); 27 if( !ifile ) { cerr << "Invalid source file!\n"; return -1; } 28 29 ofile.open( argv[2],ios::app ); 30 cout << "Copying file..."; 31 32 while( ifile.read((unsigned char*)buff,1) ) { 33 ofile.write( (unsigned char*)buff,1 ); 34 cout << '.'; 35 } 36 ifile.close( ); 37 ofile.close( ); 38 cout << "." << '\n' << "Done!\n"; 39 return 1; 40 } 41 void usage ( char *arg ) { 42 cout << "Usage: " << arg << " <source_file> <dest_file>\n"; 43 exit(1); 44 } 45 46

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS