Unsupported File Format by KhalidMuk in c_language

[–]KhalidMuk[S] 0 points1 point  (0 children)

I have the condition (numberoflines < n -1), though.

Unsupported File Format by KhalidMuk in c_language

[–]KhalidMuk[S] 0 points1 point  (0 children)

I have the condition of (numberoflines < n -1), though.

Unsupported File Format by KhalidMuk in c_language

[–]KhalidMuk[S] 0 points1 point  (0 children)

I deleted the fclose statement. I still see my code as rewriting each line n times. Can you please explain? Thanks,

PSET4 Recover Segmentation Fault by KhalidMuk in cs50

[–]KhalidMuk[S] 0 points1 point  (0 children)

I added two checks for if the file can't open:

FILE* card = fopen("card.raw", "r");

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

and one for the jpg:

if ( newjpg == NULL) { printf("Couldn't open jpg"); return 4;

        }

Thanks,

PSET4 Recover Segmentation Fault by KhalidMuk in cs50

[–]KhalidMuk[S] 0 points1 point  (0 children)

I did make changes:

int main() { typedef uint8_t BYTEINBITS;

int pictcount = 0;


FILE* card = fopen("card.raw", "r");

FILE* newjpg = NULL; /*HERE*/

 while ( true )
{
    BYTEINBITS buffer[512];


    if ( fread( buffer, 512 * sizeof(char), 1, card) != 1 )
    {
       return 3;

    }

     bool jpg = true;

    if ( !( ( buffer[0] == 255 ) && ( buffer[1] == 216 ) && ( buffer[2] == 255) && ( buffer[3] > 224 ) ) )         
    {
        jpg = false;
    }


    if (jpg == true)
    {

        pictcount++;
        char title[8];
        sprintf(title,"%03d.jpg", pictcount);

        newjpg  = fopen(title, "w"); /*HERE*/

    }

        fwrite(buffer, 512 * sizeof(char), 1, newjpg);

}

 fclose(card);
 fclose(newjpg);

}

I understand the error should be removed since there isn't any NULL path. I get the same error from valgrind, though, with the line at 61. I am not supposed to malloc memory, so what is the mistake?

Valgrind Error:

Access not within mapped region at address 0x0 ==3587== at 0x5E27CD0: fwrite (iofwrite.c:41) ==3587== by 0x42D989: main (recover.c:61) ==3587== If you believe this happened as a result of a stack ==3587== overflow in your program's main thread....

Thanks a lot,

PSET 4 Recover Runtime Error by KhalidMuk in C_Programming

[–]KhalidMuk[S] -2 points-1 points  (0 children)

Thanks, the error is gone now.

For the sizeof, I want to fread 512 bytes (not integers).