473,508 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy files in c++

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

Jun 1 '06 #1
3 4656
<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
Jun 1 '06 #2
Mike Wahler wrote:


#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


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/
Jun 1 '06 #3

Peter Jansson wrote:
Mike Wahler wrote:


#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


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();

Jun 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8211
by: Mike | last post by:
I want to be able to copy a directory of files (all .HTM files) from a network drive to a local drive on the machine c:\HTMFiles , How can i do that? I tried File.Copy(source, dest) but i need...
6
1626
by: Wayne Wengert | last post by:
I am using VSNET 2003 to build an ASP.NET/VB set of pages. There are currently about a dozen aspx pages. When I make even a minor change to one page I currently rebuild the solution, copy the...
3
2174
by: Johnny | last post by:
Hi, I have created an ASP.NET application (let's call it FooBar) with VS.NET on my local machine, residing in http://localhost/FooBar. Deploying it to another folder on my machine works well...
2
1626
by: Steve Franks | last post by:
The Copy Web tool provided with VS.NET 2005 is very convenient. However every once in a while it seems to think the files on the remote server have changed, which they have not. Then when I use...
5
20998
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
8
2905
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
1
4124
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again,...
13
2584
by: jim | last post by:
Is there a way (using VB.Net or C#) to copy open or locked files? Thanks! jim
1
3861
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
1
4248
by: =?Utf-8?B?UmFkZW5rb19aZWM=?= | last post by:
I am using standard File.Copy(source,dest,true) method in C# and I have problem with copying large number of files. Here is my code: foreach (FileInfo file in files) {...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.