Quote Originally Posted by VeNiX View Post
So I should use sprintf() outside of the while loop to initialize all filenames first? or did you mean I should use something else entirely?
You should generate the file names up front before you enter code that is critical in terms of speed. You try to limit or eliminate the number of slow I/O or library calls in such code.

It may even be faster to generate all of your data in memory, and then once everything is done, write the whole memory block to the file at the end. Stuff like this:
Code:
if (found == false && num != 0)
         {
            fwrite(buffer, BLOCK, 1, jpeg);
         }
Code:
  if (num == 1)
            {
                jpeg = fopen(fileName, "w");
                fwrite(buffer, BLOCK, 1, jpeg);
            }
            else
            {
                fclose(jpeg);
                jpeg = fopen(fileName, "w");
                fwrite(buffer, BLOCK, 1, jpeg);
            }
Instead of immediately writing to a file, write to a some designated memory block, or even a memory mapped file, or something that goes to RAM and not the disk. Then when you are finished, then you create the new file and write the memory contents to a file.

Regards,

Paul McKenzie