gpt4 book ai didi

将一个图像文件复制到另一个图像文件

转载 作者:行者123 更新时间:2023-11-30 21:36:32 24 4
gpt4 key购买 nike

我正在尝试将一个图像文件的内容复制到另一个图像文件。但不知何故,我的代码创建了一个空图像。这是代码

void main() {
FILE* i = fopen("index.jpg", "r");
int size = 0;

int index = 0;
char buff[1024];
FILE* j = fopen("abc.jpg", "w+");
while (!feof(i)) {
fread(buff, 1, 1024, i);
fwrite(buff, 1, 1024, j);
index += 1024;
fseek(i, index, SEEK_SET);
seek(j, index, SEEK_SET);
}
fclose(i);
fclose(j);
FILE* picture = fopen("abc.jpg", "r");
fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);
printf("Total Picture size: %i\n", size);
}

最佳答案

你的代码几乎可以工作了。

似乎不需要查找部分,因为偏移量会自动增加。请确保仅写入读取的字节数,否则目标图像的长度可能会大于源图像,但您可以打开它。

int main()
{
FILE *i = fopen("index.jpg", "r");
int size=0;

int index=0;
char buff[1024];

FILE *j = fopen("abc.jpg", "w+");
while(!feof(i))
{
int len = fread(buff, 1,1024,i);
fwrite(buff,1,len,j);
//index += 1024;
//fseek( i,index, SEEK_SET );
//fseek( j,index, SEEK_SET );
}
fclose(i);
fclose(j);

FILE *picture=fopen("abc.jpg","r");
fseek(picture, 0, SEEK_END);
size = ftell(picture);
fseek(picture, 0, SEEK_SET);

printf("Total Picture size: %i\n",size);
return 0;
}

关于将一个图像文件复制到另一个图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49207739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com