473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 15699
"utab" <um********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.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("destination.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("destination.txt");
dst << src.rdbuf();


I think the more expressive formulation of the last line is

std::copy(std::istreambuf_iterator<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.com> <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.com> <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
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...
1
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...
11
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 =...
10
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...
3
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
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...
0
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...
2
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.