0

So far I've gotten to the point where I can recover one image. I'm to the point where I need to find a good way to make dynamic variable names. I thought of one way to do it, but it seems rather stupid. Is there an easier way to get the digits from the fileCount into the first 3 spots of the fileName character array?

    char fileName[8];

    int digit1 = 0;
    int digit2 = 0;
    int digit3 = 0;

    char char1 = '.';
    char char2 = 'j';
    char char3 = 'p';
    char char4 = 'g';

    // IS THERE A BETTER WAY DO DO THE FOLLOWING 3 CONDITIONAL STATEMENTS?
    if (fileCount < 10)
    {
        digit3 = fileCount;
    }
    if (fileCount < 100)
    {
        // find fileCount digits in 1's and 10's places
        // digit3 = digit in 1's place
        // digit2 = digit in 10's place
    }
    if (fileCount < 1000)
    {
        // find fileCount digits in 1's and 10's and 100's places
        // digit3 = digit in 1's place
        // digit2 = digit in 10's place
        // digit1 = digit in 100's place
    }

    sprintf(fileName, "%i%i%i%c%c%c%c", digit1, digit2, digit3, char1, char2, char3, char4);

    printf("%s\n", fileName);
0

if you wanted the number 12 to print with two leading zeroes

printf("%04d", 12);

1
  • Ah, I forgot to mention I was doing this in C. – BigTJ Jun 7 '20 at 23:05

You must log in to answer this question.

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