Tried and true simple file copying code in C?

前端 未结 7 1199
醉梦人生
醉梦人生 2020-12-03 09:09

This looks like a simple question, but I didn\'t find anything similar here.

Since there is no file copy function in C, we have to implement file copying ourselves,

7条回答
  •  广开言路
    2020-12-03 09:16

    One thing I found when implementing my own file copy, and it seems obvious but it's not: I/O's are slow. You can pretty much time your copy's speed by how many of them you do. So clearly you need to do as few of them as possible.

    The best results I found were when I got myself a ginourmous buffer, read the entire source file into it in one I/O, then wrote the entire buffer back out of it in one I/O. If I even had to do it in 10 batches, it got way slow. Trying to read and write out each byte, like a naieve coder might try first, was just painful.

提交回复
热议问题