Linux 用fread fwrite 复制一张图片,c语言
时间: 2023-07-12 16:24:43 浏览: 104
C语言fread和fwrite
4星 · 用户满意度95%
可以使用以下代码复制一张图片:
```c
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main() {
FILE *source_file, *target_file;
char buffer[BUFFER_SIZE];
source_file = fopen("source_image.jpg", "rb");
if (source_file == NULL) {
printf("Failed to open source file.");
return 1;
}
target_file = fopen("target_image.jpg", "wb");
if (target_file == NULL) {
printf("Failed to open target file.");
return 1;
}
while (!feof(source_file)) {
size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, source_file);
fwrite(buffer, 1, bytes_read, target_file);
}
fclose(source_file);
fclose(target_file);
return 0;
}
```
请将代码中的 "source_image.jpg" 和 "target_image.jpg" 替换为你实际的文件名,并确保目标文件的目录已存在。
阅读全文