Skip to content

Instantly share code, notes, and snippets.

@devongovett
Last active December 10, 2015 09:58
Show Gist options
  • Save devongovett/4417385 to your computer and use it in GitHub Desktop.
Save devongovett/4417385 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char * number2filename(char *start, int digits, int num, char *end) {
int size = strlen(start) + strlen(end) + 1;
int formatSize = size + 3 + log10(digits) + 1;
char *format = (char *)malloc(formatSize);
snprintf(format, formatSize, "%s%%0%dd%s", start, digits, end);
size += digits;
char *s = (char *)malloc(size);
snprintf(s, size, format, num);
free(format);
return s;
}
int main(int argc, char *argv[]) {
printf("%s", number2filename("capture_", 5, 15, ".jpeg"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment