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


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 //Rigel Freden (rigelf@angelfire.com) 2 3 #include<iostream> 4 #include<fstream> 5 #include<string.h>//c header for strcmp 6 7 int main(int argc,char* argv[]) 8 { 9 ifstream ifile; 10 ofstream ofile; 11 12 char buf[1]; 13 14 if(!strcmp(argv[1],argv[2]))//second and third arguments the same 15 { 16 cerr<<"can't copy a file to itself\n"; 17 return -1;//quit--failure 18 } 19 20 ifile.open(argv[1]);//open the files with the given names 21 ofile.open(argv[2]); 22 //need to handle disk error here 23 24 //this is byte by byte, so rather slow 25 while(ifile.read((unsigned char*)buf,1))//as soon as it is read, 26 ofile.write((unsigned char*)buf,1);//write it to the copy 27 28 cout<<"success, I think\n"; 29 return 0; 30 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS