No, your code only partially works. It is creating 50 files, but they don't contain valid images. Also, if you run ls -al *.jpg
, you'll see that every file is 512 bytes long. The code is only writing the signature blocks out to the files.
After the first signature is found, all the datacode opens a file and writes 512 bytes to that follows is valid image datafile. The code needs to keep reading the data and addingreal problem is that it tois reopening the output file until the next signaturein 'w' mode. This means that each time a 512 byte block is found (at which point it should closeread, the existing file and openis deleted a new one.)
You need to add code to handlefile is created, and the intermediate imagecurrent 512 bytes is written out - but the previous data. There is none inlost because the code nowfile was deleted when the latest fopen is executed.
That's whatThe logic is wrong with your code, and yesmostly sound, it wouldbut the output file should only be opened once and only when a duplicate because thisnew signature is a very common problem. But that's okfound. If you weren't able to recognize For all the problemdata between signature blocks, then you wouldn't understand why it has been seen before. ;-)
You could also improve the code by incorporating the fread into the while statement. You should learn that while(1 == 1)
or while(true)
essentially creates an infinite loop. Infinite loopsdata blocks should almost alwayssimply be avoided, unless it is truly intendedwritten to run foreverthe already opened file. (Deliberate infinite loops are more often used in OS operations
Also, but generally not in application codethe signature check is only checking the first 3 bytes. If you're creating an infinite loop, you should be ableIt needs to clearly articulate whycheck the 4th byte too.)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)