Need a project done?

C++ Programming Developer

Search This Blog

Copy one file to another file C++

'Copy one file to another file in C++' program is given below.
You have to write copy c:/source.txt c:/destination.txt to copy contents of file source.txt to destination.txt.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void Write(string, string);

int main(){
cout << "Write: copy srcfile destfile.cpp\nYou can give complete path as well.\n\n";
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
if (strcmp(s1.c_str(),"copy") != 0){
cout << s1 << " is not a valid command. Only valid command for this program is copy." << endl;
}
Write(s2,s3);
return 0;
}
void Write(string s1, string s2){
char line[100];
ifstream is(s1.c_str());
ofstream os(s2.c_str());

if (is.is_open() && os.is_open()){
while (!is.eof()){
is.getline(line,100,'\n');
os << line << endl;
}
}
else{
if (!is.is_open()){
cout << endl << s1 <<" couldn't be opened. Creat and write something in a.txt, and try again." << endl;
}
else{
cout << endl << s2 <<" couldn't be opened. Creat and write something in a.txt, and try again." << endl;
}
}
}

Visit a simpler program:

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."