0

Can somebody please help me with the code, I couldnt figure out why the pictures cannot open. It's didn't show any error. Thanks.

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

int main(void)
{

    FILE *memorycard = fopen("card.raw","r"); 
    if( memorycard == NULL)
    {
        fprintf( stderr, "Cound not open forensic memorycard.");
        return 2;
    }

    //
    unsigned char readblock[512];  

    FILE* picture;
    picture = NULL; 

    char filename[8];

    int filecount;
    filecount = 0;

    int jpgfound;
    jpgfound = 0;



    while ( fread( readblock, 512, 1, memorycard) != 0)
    {
        //JPEG found
        if( readblock [0] == 0xff && readblock[1] == 0xd8 && readblock[2] == 0xff && (readblock[3] & 0xf0) == 0xe0)
        {


            if (jpgfound > 0 )
            {
                fclose(picture);
            }

            sprintf( filename, "%03i.jpg", filecount); 
            picture = fopen("filename", "w"); 

            if( picture == NULL)
            {
                fprintf(stderr, "Can't open file.\n");
            }

            printf( "%s", filename);


            filecount++;

            fwrite( &readblock, 512, 1, picture); //&
            jpgfound = 1;

        }

    }

        fclose(memorycard);

        return 0;
 }
1
  • On Stack Exchange, in the question and answer editors (but not in comments), you can select code and click the {} button or press Ctrl+K to indent the lines. Any block of lines that all start with at least four spaces is considered a code block. That's why the main function is interpreted as such, but your first and last lines aren't. Select the whole code and click the button, so your whole code looks nice.
    – Blauelf
    Commented Mar 28, 2018 at 13:52

1 Answer 1

1

"filename" is a string constant, a pointer to a string of {'f','i','l','e','n','a','m','e','\0'}.

filename instead is a variable of type char[8].

Pick the right one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .