1

Pseudocode:


declare counter 
declare filename[8]

main

Open input

Declare buffer

do 

    read(512 bytes into buffer from input)

while (not start of jpeg)


do

    write filename

    open output file with filename (using "a")

    do 

       write to output file
       read from input file

    while (not start of jpeg)

    close file

    add to counter

while (more than 1 block left)

close input

return 0;

This causes a segmentation fault

Valgrind - 1 error 1 context
Leak summary - 704 bytes in 2 blocks
352 by main line 21 (open input file)
352 by main line 48 (open output file) 

I have an issue with opening my files but I can't figure out where for the life of me. Any clues would be greatly appreciated.

Cheers

Adam

4
  • Adam, in the inner-most loop the pseudo code shows read from output file. Do you really mean read from input file? Commented Aug 8, 2014 at 13:42
  • Yes, i'll change it, thanks Luke Commented Aug 8, 2014 at 13:45
  • How do you check if there's more than 1 block left?
    – kzidane
    Commented Aug 8, 2014 at 14:09
  • fread(&buffer, 512, 2, inptr) != 1
    – user1369
    Commented Aug 8, 2014 at 14:12

1 Answer 1

2

You're trying to read 2 elements of size 512 bytes into a buffer which I assume is of size 512 bytes only. That's definitely one reason why your program segfaults.

Also, fread() returns the number of bytes that were successfully read if the number of elements is 1. I don't really know why you're checking whether the return value is not equal to 1 in this case.

And lastly, you should check whether the EOF was already reached.

5
  • feof(inptr) == 0 (indicates end of file was reached?) Can I use this as my condition to end the loop? Commented Aug 9, 2014 at 11:10
  • @AdamWilson yes, but be wary that this function returns true if the EOF was already reached. Otherwise, it returns false. It doesn't check whether the next thing to read is EOF.
    – kzidane
    Commented Aug 9, 2014 at 11:21
  • In this context, if the last thing that was read was the last 512 bytes then it will return true? My code no longer segfaults though it is still buggy and it is still leaking 704 bytes in two blocks. It produces 577 images which I guess is too many. I have a feeling I have an issue in finding the start of the jpg, my condition is while (buffer[0] != jpgstart1[0] && buffer[1] != jpgstart1[1] && buffer[2] != jpgstart1[2] && (buffer[3] != jpgstart1[3] || buffer[3] != jpgstart2[3])); Would this work? Commented Aug 9, 2014 at 11:28
  • @AdamWilson yes, it will return true if you called it right after the last 512 bytes were read. It's really hard to tell what's going one without seeing some code. There are only 50 JPGs to be recovered. Feel free to email me with your code at [email protected]!
    – kzidane
    Commented Aug 9, 2014 at 11:59
  • Brilliant thanks! Commented Aug 11, 2014 at 12:32

You must log in to answer this question.

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