Skip to content

Instantly share code, notes, and snippets.

@angch
Created January 15, 2015 13:13
Show Gist options
  • Save angch/518af1b8873b982e74e6 to your computer and use it in GitHub Desktop.
Save angch/518af1b8873b982e74e6 to your computer and use it in GitHub Desktop.
// Heya, https://forum.lowyat.net/topic/3470685/+0
// Tested on clang-600.0.56 and gcc 4.8.2
// TWVzc2luZyB3aXRoIG5vb2JzLCBvbmUgZXhhbXBsZSBhdCBhIHRpbWUsIHllcywgSSBoYXZlIGJldHRlciB3b3JrIHRvIGRvLg==
#include <stdio.h>
#include <limits.h>
struct rank {
char member[50];
int mark;
};
// Loops are for weaklings, function^Wtail recursion ftw!
void doScore(FILE *fp, int n, struct rank *player) {
printf("Enter you[sic] name: ");
// sizeof player's member. lol.
if (!fgets((*player).member, sizeof((*player).member), stdin)) {
return;
}
// Handle disk full gracefully
if (!fprintf(fp, "%d) Name:%s Score:%d\n",n, (*player).member, (*player).mark)) {
return;
}
if (!fprintf(fp, "_________________\n\n")) {
return;
}
// We don't want integer wrap around of course!
if (n == INT_MAX) {
return;
}
doScore(fp, n + 1, player);
}
int main(int argc, char *argv[]) {
int score = 20;
struct rank player;
FILE *fp;
player.mark = score;
fp = fopen("result.txt", "a+");
if (!fp) {
// Be nice, we don't have exception handling in C
fprintf(stderr, "Don't lar try to write to stuff you have no permission for.\n");
return -1;
}
doScore(fp, 1, &player);
// RAII?
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment