Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- static bool invalid_level(int);
- static int get_level(const char *);
- static void set_level(const char *, int);
- static const char *level_file = "typing.ini";
- int main(void) {
- puts("set 1"); set_level(level_file, 1);
- printf("level: %d\n", get_level(level_file));
- puts("set 10"); set_level(level_file, 10);
- printf("level: %d\n", get_level(level_file));
- puts("set 5"); set_level(level_file, 5);
- printf("level: %d\n", get_level(level_file));
- }
- static bool invalid_level(int level) {
- return level < 1 || 8 < level;
- }
- static int get_level(const char *fname) {
- FILE *in = fopen(fname, "r");
- int level;
- fscanf(in, "%d", &level);
- fclose(in);
- if (invalid_level(level)) {
- return 1;
- }
- return level;
- }
- static void set_level(const char *fname, int level) {
- if (invalid_level(level)) {
- return;
- }
- FILE *out = fopen(fname, "w");
- fprintf(out, "%d\n", level);
- fclose(out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement