0

This is an odd one. I've managed the get the code to work and output all 50 jpgs as expected with the exception of the first one. It's creating the file but not naming it correctly (not even adding .jpg onto the end) but if I rename it I can see it as the expected photo.

I've run the code through gdb and I'm still confused. 'num' is iterating as expected but the filenames are being named as if it's num - 1. On the first pass gdb says that num = 0 but the filename doesn't output correctly at all.

Hope that makes sense to someone?

The relevant code is:

                if (jpgout == NULL)
                {
                    jpgout = fopen(title, "w");
                    sprintf(title, "%03d.jpg", num);
                    num++;
                }
                else
                {
                    fclose(jpgout);
                    jpgout = fopen(title, "w");
                    sprintf(title, "%03d.jpg", num);
                    num++;
                }

[EDIT: irrelevant code removed.]

2
  • What is happening with num before the if statement? Can you please edit the question and add the declaration statement for num and anything else that touches num ? Also, please add the same for jpgout. – Cliff B May 29 '16 at 22:49
  • edited above - I've included almost the whole code – MelJ79 May 29 '16 at 22:59
0

I should have seen the problem before I asked for the edit. You have a sequencing problem. The purpose of the sprintf() call is to put the name of the file to be created inside the var title before opening the file. Since you have the two lines reversed, the first fopen() is using whatever garbage data is in title, not the correct name. After that, an appropriate file name is in title just waiting for the next call.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much! I've passed check50 now and also have a better understanding of sprintf! You're a star. – MelJ79 May 30 '16 at 8:36

You must log in to answer this question.

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