The whole file is composed of 512 byte blocks so you should always read exactly 512 bytes.
It goes something like this:
- Read 512 bytes from file.
- Check whether bytes starting have a JPEG signature
- YES: close current file and create a new one
- NO: write to the current file if it is not null (e.g. at the very start of the loop)
- Go to the first step and repeat until the whole file is read.
I hope this will help you :)
PS: also I would suggest you to save the number which is returned by fread (number of bytes it's actually read) and pass it to fwrite as the number of bytes to be written. If not, when reading last chunk of the last file, you can get less that 512 bytes and other bytes will remain intact, thus you will write a part of the previous chunk after the actual end of the last foto.
I'm not sure that this issue may cause trouble, because I took this approach in the first time and everything worked like a charm. It's good practice nonetheless :)