In C this is just
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "cJSON.h"
cJSON *get_solutions(const char *path) {
FILE *fp = fopen(path, "r");
cJSON *solutions = NULL;
if (fp) {
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
rewind(fp);
char *buffer = malloc(len + 1);
if (!buffer) {
fclose(fp);
return NULL;
}
fread(buffer, 1, len, fp);
buffer[len] = '\0';
fclose(fp);
solutions = cJSON_Parse(buffer);
free(buffer);
if (!solutions) {
solutions = cJSON_CreateObject();
}
} else {
// File does not exist: create it
fp = fopen(path, "w");
if (fp) {
fclose(fp);
}
solutions = cJSON_CreateObject();
}
return solutions;
}