我有一些CS50 Pset4的半工作代碼。如果你運行它,你會看到它恢復27 jpg文件,但只有第一幾行是可見的。CS5Ox Pset4恢復:代碼只能恢復部分圖像
有人能指出我正確的方向嗎?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
typedef uint8_t BYTE;
int main (int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover infile\n");
return 1;
}
// open file to be recovered
FILE *infile = fopen(argv[1], "r");
if (infile == NULL)
{
fprintf(stderr, "Could not open infile.\n");
return 2;
}
// temp storage for blocks
BYTE buffer[512];
// variable to store filename
char filename[8];
//store number of recovered files
int n = 0;
// temp storage for outfiles
FILE* outfile = NULL;
// iterate over all blocks of memory until end of SD card is reached
while (fread(buffer, 512, 1, infile) != 0)
{
// read one block
fread(buffer, 512, 1, infile);
// check if block is start of jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
//close previous file if already open
if(outfile != NULL)
{
fclose(outfile);
}
// creeate new outfile
sprintf(filename, "%03i.jpg", n);
outfile = fopen(filename, "w");
// write block to outfile
fwrite(buffer, 512, 1, outfile);
n++;
}
else
{
// write block to current outfile
if(outfile != NULL)
{
fwrite(buffer, 512, 1, outfile);
}
}
}
//close last outfile
fclose(outfile);
//close infile
fclose(infile);
}
您的文件名太短(空終止?)。花費多個小時......好吧,有點浪費。 –
@Eugene,每個規範的文件名應該是XXX.jpg。考慮到空終止我會認爲我需要8個字符來存儲文件名。還是我錯過了明顯的東西? (我以0經驗開始了cs50 :)) – LegalExperience
@Mark,你可以在這裏找到我的輸出示例:http://imgur.com/3Nf1Ui4。我不知道輸出應該是什麼樣子,但它應該是一個清晰的圖景。 – LegalExperience