0

I've been trying to figure out what's going on for a week. Thought maybe someone would be kind enough to point me in the right direction. I get a segmentation fault. I'm not sure the code would work anyway, but being as I can't take it any further at the moment, maybe someone can at least give me a general idea of what I have messed up. I'd really appreciate any input. Maybe fresh eyes and perspective will help!

https://www.dropbox.com/s/suvq8o56ucfi38y/recover.c?dl=0

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

int main(void)
{
// hard code card 
FILE* file = fopen("card.raw", "r");
char filename[8];
FILE* outfile = fopen(filename, "w");

//error checking
if(file == NULL)
{
   fclose(file);
   printf("There was an error, please try again \n");
   return 1;
}
    //open outfile and count to keep track
    int count = 0;
    int open = 0;
    // opening arrays to search the first four bytes
    unsigned int jpgcompare[4] = {0xff, 0xd8, 0xff, 0xe0};
    unsigned int jpgcompare2[4] = {0xff, 0xd8, 0xff, 0xe1};
    //start reading the card
    unsigned int buffer[512];
    unsigned int check[4];
    fread(buffer, 512, 1, file);
    //read until the end of the card
    while(fread(buffer, 512, 1, file) > 0)
    {
        if(file != NULL)
        {
            for(int i = 0; i < 4; i++)
            {
                //assigning first four bytes of what is held in buffer to check
                check[i] = buffer[i];
            }
            // checking for jpeg signature
                if((memcmp(jpgcompare, check, 4)== 0) || (memcmp(jpgcompare2, check, 4)) == 0)
                {
                    // make filename

                    sprintf(filename, "%03d.jpg", count);
                        if(open == 0)
                        {

                            fwrite(buffer, sizeof(buffer), 1, outfile);
                            count++;
                            open = 1;
                        }
                        else if(open > 0)
                        {
                        fclose(outfile);
                        outfile = fopen(filename, "w");
                        fwrite(buffer, sizeof(buffer), 1, outfile);
                        count++;
                        }

                }
              else

                    {
                            fwrite(buffer, 512, 1, outfile);
                    }  
        }
    }
if(outfile)
{
    fclose(outfile);

}

fclose(file);
return 0;
}

Here is what valgrind says

==24421== Use of uninitialised value of size 4
==24421==    at 0x41933357: fwrite (iofwrite.c:41)
==24421==    by 0x804891A: main (recover.c:60)
==24421== 
==24421== Use of uninitialised value of size 4
==24421==    at 0x41933392: fwrite (iofwrite.c:42)
==24421==    by 0x804891A: main (recover.c:60)
==24421== 
==24421== Use of uninitialised value of size 4
==24421==    at 0x419333B6: fwrite (iofwrite.c:43)
==24421==    by 0x804891A: main (recover.c:60)
==24421== 
==24421== Invalid read of size 4
==24421==    at 0x419333CC: fwrite (iofwrite.c:43)
==24421==    by 0x804891A: main (recover.c:60)
==24421==  Address 0x6568541c is not stack'd, malloc'd or (recently) free'd
==24421== 
==24421== 
==24421== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==24421==  Access not within mapped region at address 0x6568541C
==24421==    at 0x419333CC: fwrite (iofwrite.c:43)
==24421==    by 0x804891A: main (recover.c:60)
==24421==  If you believe this happened as a result of a stack
==24421==  overflow in your program's main thread (unlikely but
==24421==  possible), you can try to increase the size of the
==24421==  main thread stack using the --main-stacksize= flag.
==24421==  The main thread stack size used in this run was 8388608.
==24421== 
==24421== HEAP SUMMARY:
==24421==     in use at exit: 352 bytes in 1 blocks
==24421==   total heap usage: 1 allocs, 0 frees, 352 bytes allocated
==24421== 
==24421== LEAK SUMMARY:
==24421==    definitely lost: 0 bytes in 0 blocks
==24421==    indirectly lost: 0 bytes in 0 blocks
==24421==      possibly lost: 0 bytes in 0 blocks
==24421==    still reachable: 352 bytes in 1 blocks
==24421==         suppressed: 0 bytes in 0 blocks
==24421== Rerun with --leak-check=full to see details of leaked memory
==24421== 
==24421== For counts of detected and suppressed errors, rerun with: -v
==24421== Use --track-origins=yes to see where uninitialised values come from
==24421== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
Segmentation fault
1

On line 60 your program tried to fwrite to a outfile which hasn't been fopen'ed yet.
You need to make sure your program fopens it first before fwrite'ing to it.

0

You must log in to answer this question.

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