c语言编程实现linux cp命令
时间: 2023-10-08 22:10:05 浏览: 198
c语言编程实现Linux cp命令可以参考下面的代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *source, *destination;
char ch;
if (argc < 3) {
printf("未传入源文件路径或者目标文件路径\n");
exit(1);
}
source = fopen(argv[1], "r");
if (source == NULL) {
printf("打开源文件失败!\n");
exit(1);
}
destination = fopen(argv[2], "w");
if (destination == NULL) {
printf("打开目标文件失败!\n");
exit(1);
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
printf("文件复制成功!\n");
fclose(source);
fclose(destination);
return 0;
}
```
阅读全文