私は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);
}
あなたの 'filename'は短すぎます(null-termination?)。それに複数の時間を費やして...まあ、少し無駄です。 –
@Eugene、仕様ファイル名はXXX.jpgである必要があります。 null-terminationを考えると、私はファイル名を格納するのに8文字が必要だと思うでしょう。または私は何か明白な行方不明ですか? (私は以前の経験値0でcs50を開始しました:)) – LegalExperience
@マーク、私の出力の例をここに見つけることができます:http://imgur.com/3Nf1Ui4。出力がどのように表示されるべきかはわかりませんが、明確な画像でなければなりません。 – LegalExperience