diff --git a/.gitignore b/.gitignore index 4ceefe1..7ba50e9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ bin/Debug/* bin/Release/* build/Debug/* -build/Release/* \ No newline at end of file +build/Release/* diff --git a/bin/Debug/tempRecord b/bin/Debug/tempRecord index f713ab2..306b997 100755 Binary files a/bin/Debug/tempRecord and b/bin/Debug/tempRecord differ diff --git a/gmon.out b/gmon.out index 7b8ad7b..5b64d1e 100644 Binary files a/gmon.out and b/gmon.out differ diff --git a/src/main.c b/src/main.c index f9f0b43..5d0a718 100644 --- a/src/main.c +++ b/src/main.c @@ -105,7 +105,6 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data) char dateString[32]; char timeString[32]; char filename[100]; - char jsonBuffer[1024]; FILE *jsonFilePtr; struct json_object *parsed_json; @@ -135,9 +134,21 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data) fprintf(stderr, "Failed to open %s\n", filename); exit(1); } - fread(jsonBuffer, 1024, 1, jsonFilePtr); + fseek(jsonFilePtr, 0, SEEK_END); + long fileSize = ftell(jsonFilePtr); + rewind(jsonFilePtr); + char *jsonBuffer = malloc(fileSize + 1); + if (jsonBuffer == NULL) + { + fprintf(stderr, "Failed to allocate buffer for %s\n", filename); + fclose(jsonFilePtr); + exit(1); + } + fread(jsonBuffer, 1, fileSize, jsonFilePtr); + jsonBuffer[fileSize] = '\0'; //printf("raw json read in: %s \n", jsonBuffer); parsed_json = json_tokener_parse(jsonBuffer); + free(jsonBuffer); //printf("json read in: %s \n", json_object_to_json_string(parsed_json)); @@ -186,8 +197,8 @@ int8_t save_data_to_json_file(struct bme280_data *sensor_readings_data) fseek(jsonFilePtr, 0, SEEK_SET); ftruncate(fileno(jsonFilePtr), 0); fputs(json_object_to_json_string(parsed_json), jsonFilePtr); -printf("json to write out: %s \n", json_object_to_json_string(latest_reading_object)); -printf("new json to write out: %s \n", json_object_to_json_string(parsed_json)); + printf("json to write out: %s \n", json_object_to_json_string(latest_reading_object)); + printf("new json to write out: %s \n", json_object_to_json_string(parsed_json)); fclose(jsonFilePtr); return 0; }