How can I copy a file on Unix using C?

前端 未结 8 720
说谎
说谎 2020-11-27 12:39

I\'m looking for the Unix equivalent of Win32\'s CopyFile, I don\'t want to reinvent the wheel by writing my own version.

8条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 13:10

    sprintf( cmd, "/bin/cp -p \'%s\' \'%s\'", old, new);
    
    system( cmd);
    

    Add some error checks...

    Otherwise, open both and loop on read/write, but probably not what you want.

    ...

    UPDATE to address valid security concerns:

    Rather than using "system()", do a fork/wait, and call execv() or execl() in the child.

    execl( "/bin/cp", "-p", old, new);
    

提交回复
热议问题