使用标准IO函数实现将本地的图片下载在其他目录
时间: 2024-03-18 21:41:58 浏览: 16
可以使用 `fopen` 和 `fwrite` 函数将本地图片复制到其他目录。以下是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <src_file> <dst_dir>\n", argv[0]);
exit(1);
}
char *src_file = argv[1];
char *dst_dir = argv[2];
char *filename = strrchr(src_file, '/') + 1;
char dst_file[strlen(dst_dir) + strlen(filename) + 2];
sprintf(dst_file, "%s/%s", dst_dir, filename);
FILE *src_fp = fopen(src_file, "rb");
if (src_fp == NULL) {
perror("fopen error");
exit(1);
}
FILE *dst_fp = fopen(dst_file, "wb");
if (dst_fp == NULL) {
perror("fopen error");
exit(1);
}
char buffer[1024];
size_t nread;
while ((nread = fread(buffer, 1, sizeof(buffer), src_fp)) > 0) {
if (fwrite(buffer, 1, nread, dst_fp) != nread) {
perror("fwrite error");
exit(1);
}
}
fclose(src_fp);
fclose(dst_fp);
return 0;
}
```
这个程序接受两个参数:源文件路径和目标目录路径。它首先计算出目标文件路径,然后使用 `fopen` 函数打开源文件和目标文件,循环使用 `fread` 函数从源文件读取数据,使用 `fwrite` 函数将数据写入目标文件,最后关闭文件指针。