为什么sscanf不能在这里工作?

时间:2017-01-01 18:58:45

标签: c scanf cs50

我正在做一个基于c语言的问题集。源代码编译时没有任何语法错误,我也很确定语义。我试图使用GDB发现错误。这是我注意到每次迭代时tempstorage机会指向的地址内容,但第一个int保持不变(即0)。 这是cs50课程中的recover.c问题。我在cs50-stackexchagne上发布了相同程序的先前版本,但答案没有多大帮助。谢谢

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
#include <stdio.h>
#include <stdlib.h>
int main()
{   
    FILE* sdcard = fopen("card.raw", "r");
    // to check if the file is opened correctly or not
    if ( sdcard == NULL)
    {
        printf("corrupted sd card");
        return 1;
    }
    // file pointer to point to the temporary jpg
    FILE* tempjpgg;
    // for the nomenclauture of the jpg photos
    char* jpegname = (char*)malloc(8);
    // to read 512 bytes of data at a time from the sdcard
    char* tempstorage = (char*)malloc(512);
    int firstint;
    int i = 0;
    for (fread(tempstorage, 512, 1,sdcard); ; )
    {
        sscanf(tempstorage, "%d", &firstint); // to make the checking process easier
        if ( firstint >= 0xffd8ffe0 && firstint <= 0xffd8ffef)
        {
            fclose(tempjpgg);
            sprintf(jpegname, "%03d.jpg", i);
            tempjpgg = fopen(jpegname, "w");
            fprintf(tempjpgg, "%d", firstint);
            fwrite( tempstorage, 508, 1, tempjpgg);
            i++;
        }
        else if ( i != 0)
        {
            fprintf(tempjpgg,"%d", firstint);
            fwrite(tempstorage, 508, 1, tempjpgg);
        }
        fread(tempstorage, 512, 1, sdcard);
    }
    return 0;
}

0 个答案:

没有答案