Uzumaki Naruto
Uzumaki Naruto

Reputation: 39

Is there a way in C to copy all the files of a directory to another directory?

I'm hoping not to resort to reading and writing of every file. Isn't there any other approach to this which might be a little shorter and easier? I'm looking to do this on a linux machine.

Upvotes: 0

Views: 782

Answers (1)

Tony Tannous
Tony Tannous

Reputation: 14863

Use system()

system() is used to invoke an operating system command from a C/C++ program.

On windows:

#include <stdlib.h>

int main()
{
    return system("copy .\\CopyFrom\\*  .\\CopyTo");
}

Compile and run. Voilà!

enter image description here


On linux you'd do something like

return system("cp ./CopyFrom/*  ./CopyTo");

Upvotes: 2

Related Questions