0

I can't figure out what's wrong with my code. File names are created successfully but all jpegs just show the corrupt thumbnail. I'm sure it has something to do with finding the start and end of the files. Here is a snippet from where i think my code is going wrong:

// Read each 512 bytes until end of file is reached

while (fread(&data, 512, 1, diskptr) == 1)
{
    // Check for jpeg start
    if (checkStart(data, jStart, jStartOE) == true)
    {
        jFound++;
        writeJ(data, jFound - 1);
    }
    // If the first jpeg has already been found and current block is not
    // the start of a file keep writing to the same file
    else if (jFound > 0)
    {
        writeJ(data, jFound - 1);
    }
}

checkStart() is just a function that looks for the four jpeg signature bytes in the start of a 512 block and returns true if it found them and false if it didn't.

writeJ() sprintfs a name respective to the current jFound number which is the amount of jpegs found. Then it opens a file with that name and the "a" argument. Then it writes 512 bytes from the data and finally closes the file.

Sometimes when I run the program two pictures show up fine for some strange reason, but then I run it again and they're gone.

2
  • What sizes are the JPGs you are getting? Also, if you use "a" when you write, be sure that you delete all jpgs from your directory before you run your program again, as otherwise, the bytes will simply be appended to the existing file from the last time. (better to not use "a" and only close the file after all the bytes have been written to it because your program might not pass check50. All those open/closes will slow it down too much.
    – curiouskiwi
    Aug 2 '15 at 6:21
  • I made the changes you recommended. Instead of having a function to open write and close I implemented the opening, writing and closing inside the loop to avoid reopening the same files. Now I only get one jpeg and segmentation fault core dumped :(
    – epiqueras
    Aug 2 '15 at 15:20
0

This is how my loop looks now, what have I done wrong?

// Read each 512 bytes until end of file is reached
FILE* img = NULL;
// snipped by mod
            FILE* img = fopen(title, "w");

I added those printf()s to see if it was ever executing this else ifs and it isn't. newfound is printed out once and then nothing. I only get one corrupt 000.jpg. I don't understand why it's not executing the other lines of code if img is not null anymore because the fact that newfound was printed indicates that the block of code that run fopen() has been executed.

4
  • Fixed It! I was declaring a new "img" variable inside the if statement instead of assigning a new value to the "img" that already existed. That's why the original "img" was always NULL. :) Thanks for the advice curiouskiwi.
    – epiqueras
    Aug 2 '15 at 17:52
  • Please 'accept' your answer so that this question will fall off the unanswered queue.
    – curiouskiwi
    Aug 2 '15 at 19:51
  • I know, I tried but it said I have to wait two days before being able to accept my own answers.
    – epiqueras
    Aug 2 '15 at 23:41
  • sorry, meant to say that in my comment. :)
    – curiouskiwi
    Aug 2 '15 at 23:42

You must log in to answer this question.

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