JSON was getting truncated at 1K

This commit is contained in:
2026-04-13 15:43:11 -06:00
parent 9cec82ab1f
commit 876a7866ed
4 changed files with 16 additions and 5 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
bin/Debug/*
bin/Release/*
build/Debug/*
build/Release/*
build/Release/*
Binary file not shown.
BIN
View File
Binary file not shown.
+15 -4
View File
@@ -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;
}