-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.c
61 lines (53 loc) · 1.32 KB
/
generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
int main() {
//255 max for each value
uint8_t rows = (uint8_t)5;
uint8_t cols = (uint8_t)5;
char *puzzle = "01110"
"10111"
"11111"
"11101"
"01110";
char puzzle_name[] = "button";
char file_name[100] = "puzzles/";
strcat(file_name, puzzle_name);
strcat(file_name, ".bin");
FILE *file = fopen(file_name, "wb");
if (file == NULL) {
perror("Error opening file");
return 1;
}
size_t row_bytes_written = fwrite(&rows, sizeof(uint8_t), 1, file);
size_t col_bytes_written = fwrite(&cols, sizeof(uint8_t), 1, file);
if (row_bytes_written != 1 || col_bytes_written != 1) {
perror("Error writing puzzle headers to file");
fclose(file);
return 1;
}
size_t puzzle_bytes_written = 0;
unsigned char byte = 0;
int length = strlen(puzzle);
for (int i = 0; i < length; i++) {
if (puzzle[i] == '1') {
byte |= (1 << (7 - (i % 8));
}
if ((i + 1) % 8 == 0) {
puzzle_bytes_written += fwrite(&byte, sizeof(byte), 1, file);
byte = 0;
}
if (i == length - 1) {
puzzle_bytes_written += fwrite(&byte, sizeof(byte), 1, file);
}
}
if (puzzle_bytes_written != length / 8 + 1) {
perror("Error writing puzzle data to file");
fclose(file);
return 1;
}
fclose(file);
printf("Data written to file successfully.\n");
return 0;
}