Skip to content

Instantly share code, notes, and snippets.

@thebirk
Created July 7, 2018 21:26
Show Gist options
  • Save thebirk/6ff812dddacdedf10b0d025991c15d21 to your computer and use it in GitHub Desktop.
Save thebirk/6ff812dddacdedf10b0d025991c15d21 to your computer and use it in GitHub Desktop.
Simple build number counter.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
if(argc != 2) {
printf("Usage: buildnum <file>\n");
return 1;
}
FILE *f = fopen(argv[1], "rb");
if(!f) {
f = fopen(argv[1], "wb");
if(!f) {
printf("failed to open/create file\n");
return 1;
}
}
int buildnumber = -1;
int filled = fscanf(f, "%d", &buildnumber);
if(filled != 1) {
buildnumber = 1;
} else {
buildnumber++;
}
f = freopen(argv[1], "wb", f);
fprintf(f, "%d", buildnumber);
fflush(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment