So i seem to be stuck in an infinite loop, never being able to find the end of my file. I have tried these two statements to try and find the end of the loops but neither work, I'm not sure why.

while(fgets((char *)buffer, sizeof(buffer), inptr) != NULL)

Also when i try to debug my program with debug50 the program ends between these lines and exits, i have tried stepping into the function and stepping over but neither work. Which is a real pain when trying to figure out whats wrong

fread(buffer, 512, 1, inptr);

while (checkBuffer(buffer) == false);

I used a printf to see how many loops the program does, which is 50 before 'File size limit exceeded'error happens. 50 .jpg files are produced all of which are fine apart from the last one due to an unsupported file type.

Any help with these three issues is greatly appreciated

Issue was fixed by using this code instead

while (checkBuffer(buffer) == false && !feof(inptr));

1 Answer 1


This code will find and produce the first 49 images. It will go into an infinite loop after it writes the last block of the last jpg. The program aborts when it gets the "file size limit exceeded". It has already written lots of junk to the 50th file, making it an invalid file.

Here is your infinite loop:

                // Write the buffer to the jpeg
                fwrite(buffer, 512, 1, outptr);

                 // Read 512 bytes from card.raw and store it in buffer
                fread(buffer, 512, 1, inptr);

            while (checkBuffer(buffer) == false);

After it writes the last block of the last jpg, it reads the next 512-byte block. The last jpg has been processed, so it does not find another jpg signature. Since it does not find any more jpg sigs, the while test while (checkBuffer(buffer) == false); will always be true and the program keeps writing and reading 512-byte blocks. It never finishes, but it is killed by the system when it gets the "file size limit exceeded" message.

  • I'm sorry i don't understand where this is wrong. What I think this code does is; fwrite will then take the data in buffer and write it to a jpeg, fread takes the next 512 bytes and stores it temporarily in the variable buffer, checkbuffer will check if this next 512 bytes is a new jpeg or for the old jpeg. You say that that i probably shouldn't get any images of out this, however i have 50 jpegs except the last one doesn't open. Bare in mind i may be completely wrong with my understanding of this code, if I'm wrong where am i wrong thanks Jan 18, 2017 at 20:01
  • Sorry if I gave the impression that the program would not produce any jpgs. The only problem is on the last one. Answer has been rewritten. I hope it is now more clear and helpful. Jan 18, 2017 at 22:21

You must log in to answer this question.

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