用C语言代码完成文件备份工作
时间: 2023-03-28 13:03:28 浏览: 100
你好,以下是用 C 语言代码完成文件备份工作的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source file> <destination file>\n", argv[]);
return 1;
}
FILE *src_file = fopen(argv[1], "rb");
if (src_file == NULL) {
printf("Failed to open source file: %s\n", argv[1]);
return 1;
}
FILE *dst_file = fopen(argv[2], "wb");
if (dst_file == NULL) {
printf("Failed to open destination file: %s\n", argv[2]);
fclose(src_file);
return 1;
}
char buffer[1024];
size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > ) {
fwrite(buffer, 1, bytes_read, dst_file);
}
fclose(src_file);
fclose(dst_file);
printf("File backup completed successfully.\n");
return ;
}
```
希望能对你有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)