gpt4 book ai didi

c - pset 3 - 恢复 - 代码仅输出一个图像并退出调试 50

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:47 27 4
gpt4 key购买 nike

我遵循了 pset 3 recover 的伪代码,我的代码只输出一个图像,调试器 (debug50) 在 number = fread(buffer, 1, 512, file); 中循环 4 次后退出; 行。

我检查了输出 jpeg 的十六进制值,结果发现它与原始文件相比也不正确。我可以辨别图像,但一些十六进制数有小的偏差。我

// open card file 
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
fprintf(stderr, "Couldn't open %s", argv[1]);
return 2;
}

// buffer
unsigned char *buffer = malloc(513);
buffer[512] = 0x00;

// number
int number = 512;

// bool
int jpegFound = 0;

// jpeg number
int jpegNum = 0;

// jpeg name
char *jpegName = malloc(8);
jpegName[7] = 0x00;

// jpeg file pointer
FILE *jpegPtr;

// iterate through all the blocks of the card
while (1)
{
// read 512 bytes into a buffer
number = fread(buffer, 1, 512, file);

// break out of the loop if its the EOF
if (number != 512)
{
break;
}

// check whether its the start of a new jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// have we already found a jpeg?
if (jpegFound == 1)
{
// close previous jpeg file
fclose(jpegPtr);

// store the jpegName in a string
sprintf(jpegName, "%03i.jpg", jpegNum);

// open a new file with the stored jpeg name
jpegPtr = fopen(jpegName, "w");

}
else
{
// we found a jpeg finally
jpegFound = 1;

// store the jpeg Name in a string
sprintf(jpegName, "%03i.jpg", jpegNum);

// open a new file with the stored jpeg name
jpegPtr = fopen(jpegName, "w");
}

// write buffer to the jpeg
fwrite(buffer, 1, 512, jpegPtr);
}
else
{
if (jpegFound == 1)
{
// these bytes are part of the previous jpeg
fwrite(buffer, 1, 512, jpegPtr);
}
else
{
// this means that these bytes can be discarded
}
}
}

// close any remaining files
fclose(jpegPtr);

// free the memory we used in malloc
free(buffer);
free(jpegName);

// return 0
return 0;

./recover card.raw 的预期输出是很多图像,但只有一张图像看起来不错,但它的一些十六进制值略有偏差。

最佳答案

我修好了。我没有增加 jpegNum ,而且,就 debug50 而言,我仍然不知道是什么导致它退出。

关于c - pset 3 - 恢复 - 代码仅输出一个图像并退出调试 50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483518/

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