copy files in c++

V

varun.chinta

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.
Thanks
 
M

Mike Wahler

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::eek:fstream out((dest_loc + filename).c_str());

if(in && out)
{
in >> std::noskipws;

std::copy(std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::eek:stream_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
 
P

Peter Jansson

Mike said:
#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::eek:fstream out((dest_loc + filename).c_str());

if(in && out)
{
in >> std::noskipws;

std::copy(std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::eek:stream_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

Hi,

Replacing the code within the if(in && out) {...} block, I often do

if(in && out)
{
out<<in.rdbuf();
}

Is there any reason why the std::copy method above is to be preferred?


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
V

varun.chinta

Peter said:
Hi,

Replacing the code within the if(in && out) {...} block, I often do

if(in && out)
{
out<<in.rdbuf();
}

Is there any reason why the std::copy method above is to be preferred?


Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/

Thank you for the suggestions, i tried using the below method and it
worked.
ifstream Source("C:/abc.txt");
ofstream Dest("C:/abc1.txt");
Dest << Source.rdbuf();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top