<va**********@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi,
I am doing a project which involves basic file operations like file
open, file copy, file rename, file remove.
can anyone suggest me how to
do a file copy. The file that i create using the file open function, i
would like to copy that same file to another location.
should i use pointer or is there a better way to do it.
#include <algorithm>
#include <fstream>
#include <ios>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
const std::string source_loc("c:/");
const std::string dest_loc("c:/xyz/");
const std::string filename("file1");
std::ifstream in((source_loc + filename).c_str());
std::ofstream out((dest_loc + filename).c_str());
if(in && out)
{
in >> std::noskipws;
std::copy(std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::ostream_iterator<char>(out));
}
if(!in.eof())
std::cerr << "Error occurred reading input\n";
if(!out)
std::cerr << "Error occurred writing output\n";
return 0;
}
Note: the values of the strings 'source_loc' and 'dest_loc'
are subject to platform restrictions for file names. (I ran
this on Microsoft Windows).
-Mike