尝试通过在 c 中一次复制 n 个字节来将文件的内容复制到另一个文件。我相信下面的代码可以一次复制一个字节,但我不确定如何使它适用于 n 个字节,尝试制作一个大小为 n 的字符数组并将读/写函数更改为read(sourceFile , &c, n)and write(destFile , &c, n),但是缓冲区似乎不是那样工作的。
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
void File_Copy(int sourceFile, int destFile, int n){
char c;
while(read(sourceFile , &c, 1) != 0){
write(destFile , &c, 1);
}
}
int main(){
int fd, fd_destination;
fd = open("source_file.txt", O_RDONLY); //opening files to be read/created and written to
fd_destination = open("destination_file.txt", O_RDWR | O_CREAT);
clock_t begin = clock(); //starting clock to time the copying function
File_Copy(fd, fd_destination, 100); //copy function
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; //timing display
return 0;
}