473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the fastest way of copying files

Hi there,

I am copying one file into another file character by character.

I wanted to learn if there is a faster way of doing this?

When files get bigger, maybe this can take a lot of time.

Thanks for the replies in advance.

Mar 27 '06 #1
8 15742
"utab" <um********@gma il.com> wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
: Hi there,
:
: I am copying one file into another file character by character.
:
: I wanted to learn if there is a faster way of doing this?
:
: When files get bigger, maybe this can take a lot of time.
:
: Thanks for the replies in advance.

For C++ streams, the easy way to copy one stream to another
is to send the source's streambuf to the output:
// #include <fstream>
std::ifstream src("source.txt ");
std::ofstream dst("destinatio n.txt");
dst << src.rdbuf();

This should perform faster than a character-by-character copy,
but actual performance is implementation-dependent.

Of course there is always a faster/more complicated/less portable
way to implement this operation.

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Mar 27 '06 #2
Ivan Vecerina wrote:
For C++ streams, the easy way to copy one stream to another
is to send the source's streambuf to the output:
// #include <fstream>
std::ifstream src("source.txt ");
std::ofstream dst("destinatio n.txt");
dst << src.rdbuf();


I think the more expressive formulation of the last line is

std::copy(std:: istreambuf_iter ator<char>(src) ,
std::istreambuf _iterator<char> (),
std::ostreambuf _iterator<char> (dst));

However, to get this as efficient as the other version is non-trivial
and effectively requires optimized algorithms for the segmented
sequences.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 27 '06 #3
However, to get this as efficient as the other version is
non-trivial
and effectively requires optimized algorithms for the segmented
sequences.


Hint: On some compilers the fstream classes are horribly slow in
comparison to fopen/fread/fwrite. For large files you should check if
you can speed up with these.
Also, copyiny byte by byte is generally a bad idea. Read to a buffer
(e.g. 4096 bytes) and write this.
Mar 29 '06 #4
Gernot Frisch wrote:
However, to get this as efficient as the other version is
non-trivial
and effectively requires optimized algorithms for the segmented
sequences.


Hint: On some compilers the fstream classes are horribly slow in
comparison to fopen/fread/fwrite. For large files you should check if
you can speed up with these.
Also, copyiny byte by byte is generally a bad idea. Read to a buffer
(e.g. 4096 bytes) and write this.


Another hint: my own implementation of IOStreams and 'std::copy()'
in particular *is* optimized to implement the algorithm use I have
shown in this thread in a form which is really fast. It is definitely
on par with any other approach you can use on the basis of the
standard C or C++ libraries.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 29 '06 #5

utab wrote:
Hi there,

I am copying one file into another file character by character.

I wanted to learn if there is a faster way of doing this?

When files get bigger, maybe this can take a lot of time.

Thanks for the replies in advance.


Look at Simple C/C++ Perfometer: Copying Files (Versions 4.x)
http://groups.google.com/group/perfo...74465da4c4e9bb

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Apr 14 '06 #6
Think about block-copy, not character.

Apr 14 '06 #7
On Thu, 13 Apr 2006 22:03:40 -0700, Alex Vinokur wrote:

utab wrote:
Hi there,

I am copying one file into another file character by character.

I wanted to learn if there is a faster way of doing this?

When files get bigger, maybe this can take a lot of time.

Thanks for the replies in advance.


If you care to use platform dependent functions then you can use memory
mapped file access. mmap() under unix maps a file to memory or a memory
region back to a file and does it much faster than userland character or
block copies.

To maintain portability you are generally limited to the stdlib routines
though.

Apr 14 '06 #8
Ni
Copying files using block's size equal to the filesystem's scluster
size is possibly is the fastest method.
Or you may try multithreading wih block copying method.

Apr 14 '06 #9

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

Similar topics

4
2960
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
1
1714
by: Sarah | last post by:
Hi, Can someone please tell me if there's a way to copy image files from one server to another using ASP and FSO? Or perhaps some other way to do it? I'm trying to collect information on books, and want to have a picture of the book associated with the description, but I want to be nice and host the pictures myself, not just link to...
11
2111
by: KK | last post by:
Hello, I am running into a problem for copying files between 2 different servers. I am using the following lines of code to do that, but I get a 'permission denied' error Set fso = Server.CreateObject("Scripting.FileSystemObject") strSourceFile = "\\server1\file1.doc" strDestFile = "\\server2\file1.doc" fso.CopyFile...
10
2807
by: Alex Vinokur | last post by:
ofstream outfile ("out"); ifstream infile ("in"); istream_iterator<char> iter(infile), eos; Is it possible to copy 'infile' to 'outfile' using 'iter' and 'eos'? -- Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html
3
2257
by: Peter van der Veen | last post by:
Hi IN VB6 there was a control which you can use to show a small avi when for instance copying files (tyhe flying papers). How can one do that in VB.NET??? Peter
1
968
by: tshad | last post by:
I am trying convert my projects to other names and was wondering if there is a list of what the different files are used for. For example, when I try to open a Project, I have the choice of the .vbproj and the .sln files. Which is the correct file to open or does it make a difference? What are they used for? Thanks,
0
1389
by: mlfblom | last post by:
Hi, I am running visual studio 2005 on a Vista Ultimate client. I have created a remote site on a Windows 2003 R2 server. This site has about 100 aspx, many App_Code files and several references to DLL's. When I open the remote site within VS 2005 on the client machine, I takes very, very long before the site is opened and the solutione...
2
1143
by: hello123hello | last post by:
I want to run lime wire but it won't let me install it. It always stops at the copying files spot. I use mozilla firefox and i was wondering how do you get Java Runtime Environment to install?
11
7690
Ali Rizwan
by: Ali Rizwan | last post by:
Hello everybody. I have a problem in fso.copyfiles in vb6.0.Please help me in copying files and how to rename them. Thanks
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
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 we have to send another system
1
1452
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.