我开始学习 C 并且我绝对不擅长它,我正在学习 CS50 课程,第 4 周(内存),我必须查看.raw的.jpeg。
当我运行我的代码时,图像会按顺序完美地创建,等等等等,但是当我打开它们时,它们是空白的(一个有图像的某些部分,但不是我想要实现的),
#include <stdio.h>
#include <stdlib.h>
void makeJPEG(int nof, unsigned char chunk[512]);
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Just give 1 argument, not less, not more.\n");
return 1;
}
printf("El nombre del archivo es -> %s\n", argv[1]);
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char chunk[512];
int nof = 0;
while (fread(chunk, sizeof(char), 512, file) == 512)
{
if (sizeof(chunk) == (sizeof(char) * 512))
{
if (chunk[0] == 0xff && chunk[1] == 0xd8 && chunk[2] == 0xff)
{
printf("\n****************************************************\n");
printf("\n\nProbably a JPEG\n");
if ((chunk[3] & 0xf0) == 0xe0)
{
printf("Definitely a JPEG\n");
char filenameMain[8];
makeJPEG(nof, chunk[512]);
sprintf(filenameMain, "%03i.jpg", nof);
printf("The file -> %s <- was created successfully\n\n", filenameMain);
printf("\n****************************************************\n");
nof++;
}
}
else
{
printf("Not a JPEG\n");
}
}
}
fclose(file);
printf("\n*Final*\n");
return 0;
}
void makeJPEG(int nof, unsigned char chunk[512])
{
FILE* img = NULL;
//Declaring a String of 8 bytes
char filename[8];
//Giving that String the name of the file
sprintf(filename, "%03i.jpg", nof);
//Writing the new jpg file
img = fopen(filename, "a");
fwrite(chunk, sizeof(char), 512, img);
//Closing the image and freeing the memory
fclose(img);
}
我认为我的问题在于我使用“块”变量的方式以及如何将其发送到“makeJPEG”,但我不知道我做错了什么,我再说一遍,我完全是个新手。
PS:对不起我的英语错误,并提前感谢您的帮助:)