0

Can I get help with this "recover" code? I keep getting a segmentation fault and I can't understand why! It's coming from the second condition (where it's written segmentation fault) in the below code but the logic looks fine to me, that is if there's a file opened already, keep writing what you read!

Thanks guys!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int main(int argc, char* argv[]) { typedef uint8_t BYTE; // TODO // open input file

FILE* file = fopen("card.raw", "r");
if (file == NULL)
{
    fclose(file);
    printf("Could not read the  card\n");
    return 1;
}

BYTE block [512];
int i = 0;
int m = 0;
int open = 0;

char s[5];

FILE* outptr = NULL;

while (!feof(file))
{   
    fread(block, sizeof(block), 1, file);

    //  first condition: the block has an image!
        if ((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1)))
        {   
                // If file is still open from last loop, close to start a new one!
                if (outptr != NULL)
                {
                    fclose(outptr);
                    printf("file closed at i = %i and m = %i\n", i, m);
                    open = 0;
                }

                //  Output files names!
                sprintf(s, "%0.3i.jpg", i);
                // Create outfile
                FILE* outptr = fopen(s, "w");
                i++;
                //marker that outfile is open
                open = 1;

                if (outptr == NULL)
                {
                    fclose(file);
                    //marker that outfile is closed
                    open = 0;
                    printf("Impossible to create file!! \n");  
                    return 2;
                }
                fwrite(block, sizeof(block), 1, outptr);
        }

        else 
        {
                //  second condition: no image starts in block AND there IS an out file opened and hasn't been closed!
                if (open == 1)
                {
                    // keep writing in file
                    // Segmentation fault error!!!
                    fwrite(block, sizeof(BYTE), 512, outptr);
                }

                //  third condition: no image starts in block AND NO out file opened!
                // Do nothing, move on to next loop!
        }
}
fclose(file);
return 0;
1

A further problem is that you are declaring the file pointer outptr two times. The first time in main, and the second time inside the while block. You don't want a second file pointer, so assign outptr to your new file like this outptr = fopen(s, "w"); (not this FILE* outptr = fopen(s, "w");)

1
  • Worked :), thanks so much! – Askar Nov 5 '15 at 22:54
0

Ah, the dreaded !feof......

In Week 4 -> Shorts -> File I/O, Jason discusses the problem with using feof in a while loop (around 7:00). He explains the correct condition (8:40) for checking end of file. Using that logic, you need to change your while condition to test that the output of fread is not NULL.

while(fread(block, sizeof(block), 1, file) != NULL)

There may be other bugs lurking in your code. But this is a good place to start.

2
  • Oh and...don't forget to take the explicit fread out of your while loop. – DinoCoderSaurus Nov 5 '15 at 14:43
  • Well thanks so much for this, it didn't work tho :/. I guess I would have run into this problem of feof later on anyways but for now I don't as the program halts due to the segmentation fault before it reaches the feof problem! It definitely has something to do with the last fwrite as when I remove it, it runs with no errors! – Askar Nov 5 '15 at 15:08

You must log in to answer this question.

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