2

What's the best way to copy a folder (and its entire contents) from adress1 (the address of the folder) to adress2 in c? I don't want to copy it to a buffer and then save it because it might contain large files

Ofer Magen
  • 942
  • 1
  • 9
  • 19

1 Answers1

1

If you're looking to implement this yourself, look into this code

If it's not that critical that you have implementation and you can use the operating system's cp command than just use system('cp',...) or system('xcopy',...) for Windows in your code.

Anyhow, every implementation will buffer some of the file for copying (you control the size of the buffer if you implement it yourself), for example:

Say you want to copy file X of size 10000 bytes
With buffer of size 100 bytes you will loop 100 times filling the buffer and writing to the target file

Ishay Peled
  • 2,783
  • 1
  • 23
  • 37
  • 1
    *"you can use the operating system's `cp` command"* - This is assuming, that the OS has a `cp` command. Windows doesn't, and since the question is specifically asking for Windows, I'm going to have to downvote this answer. – IInspectable Jul 05 '16 at 09:30
  • Will you upvoteit now? – Ishay Peled Jul 05 '16 at 09:43
  • 1
    No. The OP is asking for the *"best"* way. Invoking `system` is - arguably - the worst. Use the shell's optimized file and folder copying infrastructure. It's a lot better at reporting errors, can provide progress feedback, and is blazingly fast, when moving files within a single volume. – IInspectable Jul 05 '16 at 09:52
  • We will have to agree to disagree. I notice you didn't read the entire answer before downvoting which makes this discussion a waste of time – Ishay Peled Jul 05 '16 at 10:12
  • I read the entire proposed answer, and the shell's optimized copying/moving doesn't buffer any data, unless it must. Absolute statements are easily refuted with a single counter-example. In essence, you have a link-only answer, a poor `system()`-based solution, followed up by inaccurate statements. This is a case where the *"This answer is not useful"* option should be used. – IInspectable Jul 05 '16 at 10:25