⑴ 如何使用C語言實現JPEG圖像格式的讀取與寫入
1.圖片也是屬於文件類型的一種,圖片屬於二進制文件。使用fopen函數的二進制模式「rb」就可以打開。
2.常式:
#include<stdlib.h>
#include<stdio.h>
intmain()
{
FILE*fpPhoto,*fpText,*fpTarget;
intiRead;
charszBuf[100];
printf("請輸入第一個文件名(bmp): ");
gets(szBuf);
fpPhoto=fopen(szBuf,"rb");
printf("請輸入第二個文件名(txt): ");
gets(szBuf);
fpText=fopen(szBuf,"rb");
printf("請輸入目的文件名(bmp): ");
gets(szBuf);
fpTarget=fopen(szBuf,"wb");
if(!fpPhoto||!fpText||!fpTarget)
{
printf("打開文件失敗! ");
system("pause");
return-1;
}
while((iRead=fread(szBuf,1,sizeof(szBuf),fpPhoto))>0)
fwrite(szBuf,1,iRead,fpTarget);
while((iRead=fread(szBuf,1,sizeof(szBuf),fpText))>0)
fwrite(szBuf,1,iRead,fpTarget);
fclose(fpPhoto);
fclose(fpText);
fclose(fpTarget);
return0;
}