gpt4 book ai didi

cs50 pset4 recovery.c 需要建议

转载 作者:行者123 更新时间:2023-11-30 17:12:32 29 4
gpt4 key购买 nike

我可以使用 cs50 中关于此 pset 的一些指导。下面是我到目前为止的代码。我被困在这一点上,我不知道如何继续。当我运行该程序时,它确实输出了 16 个 jpg 文件,但我无法查看它们,所以一定有问题。欢迎任何提示。

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
// create buffer to store in
unsigned char buffer[512];

// array for filename is 8 for "000.jpg"
char jpgName[8];

// declaring counter for amount of jpegs
int jpgcounter = 0;

// open memory card file
FILE* inptr = fopen("card.raw", "r");
FILE* outptr = NULL;

if (inptr == NULL)
{
printf("Could not open %s.\n", "card.raw");
return 1;
}

// repeat until end of card
while(fread(buffer, sizeof(buffer), 1, inptr) == 1)
{

// find beginning of jpg
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
// if allready a outptr file is open close it
if (outptr != NULL)
{
fclose(outptr);
}

// create filename for jpegs
sprintf(jpgName,"%03d.jpg", jpgcounter);

// open a new file with name defined by sprintf
FILE* outptr = fopen(jpgName, "w");

// write into the outfile from buffer
fwrite(&buffer, sizeof(buffer), 1, outptr);

// increment counter for jpegs
jpgcounter++;
}
}
fclose(inptr);
return 0;
}

最佳答案

如果我正确理解你的任务,你想要这样的东西 - 请参见下文。提示:如果它不是 jpg 文件的开头,您打算做什么?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{

unsigned char buffer[512] = {0};
char jpgName[8] = {0};
int jpgcounter = 0;
FILE* outptr = NULL;

// open memory card file
FILE* inptr = fopen("card.raw", "rb");
if (inptr == NULL)
{
printf("Could not open %s.\n", "card.raw");
return 1;
}

// Start reading
while(fread(buffer, sizeof(buffer), 1, inptr) == 1)
{

// Find beginning of jpg
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
if(NULL!=outptr)
{
fclose(outptr);
outptr=NULL;
}

// Create filename for jpegs
sprintf(jpgName,"%03d.jpg", jpgcounter);

// Open a new file with name defined by sprintf
outptr = fopen(jpgName, "wb");
if (NULL == outptr)
{
printf("Could not open %s.\n", jpgName);
return 1;
}

// Write into the outfile from buffer
fwrite(buffer, sizeof(buffer), 1, outptr);

// Increment counter for jpegs
jpgcounter++;
}
else
{
// Continue writing to old file if it is not beginning of file
if(NULL!=outptr)
fwrite(buffer, sizeof(buffer), 1, outptr);
}
}

if(NULL!=outptr)
fclose(outptr);
if(NULL!=inptr)
fclose(inptr);

return 0;
}

关于cs50 pset4 recovery.c 需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31485622/

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