How can I copy a file from one directory to another in c/c++

前端 未结 7 408
闹比i
闹比i 2020-12-17 07:18

I am looking for a simple example on how to copy a file from one directory to another in C. The program should only use cross platform functions that are native to C.

7条回答
  •  甜味超标
    2020-12-17 07:59

    @Mu Qiao mentioned boost::filesystem. Specifically you'd want to look for examples of copy_file, though you can copy directories too.

    If you're interested in getting fancy with asynchronous I/O, my impression is that boost::asio has interesting implementations that take advantage of platform abilities to let you use the CPU while waiting for the disk, just as it does for the network:

    How to perform Cross-Platform Asynchronous File I/O in C++

    (Note: Lately the asio library has been coming to mind lately a lot, but I always have to offer the disclaimer that I haven't used it in a project and kind of have no idea what its limitations are. So take my mention of it with a grain of salt.)

    There are other cross-platform libraries offering this kind of functionality, if you're willing to do the buy-in, always a matter of taste and needs. I myself like Qt for a lot of reasons, and it does happen to offer a QFile::copy method:

    http://doc.qt.nokia.com/latest/qfile.html#copy

提交回复
热议问题