Skip to content

Instantly share code, notes, and snippets.

@hfm
Last active May 29, 2018 13:33
Show Gist options
  • Save hfm/fecc933d5b68a1f200854e16fe85a3f4 to your computer and use it in GitHub Desktop.
Save hfm/fecc933d5b68a1f200854e16fe85a3f4 to your computer and use it in GitHub Desktop.
#include <dirent.h>
#include <fstream>
#include <libgen.h>
#include <stdio.h>
int main(void) {
int maxidx = 0;
DIR *dir;
struct dirent *ent;
const char *base_filename = "queries.log";
const char *datadir = "log";
size_t efl = strlen(base_filename);
if ((dir = opendir(datadir)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strlen(ent->d_name) == efl + 9) {
if (strncmp(ent->d_name, base_filename, efl) == 0) {
if (ent->d_name[efl] == '.') {
int idx = atoi(ent->d_name + efl + 1);
if (idx > maxidx)
maxidx = idx;
}
}
}
}
closedir(dir);
} else {
/* could not open directory */
printf("Unable to open datadir: %s\n", datadir);
exit(EXIT_FAILURE);
}
printf("Current ID is %i\n", maxidx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment