FILE *fp = fopen(file_path, "ab");
if (fp == NULL)
{
- fopen(file_path, "wb+");
+ // 未存储分配函数funcName的返回值
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append");
+ fp = fopen(file_path, "wb+");
+ if (fp == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for write");
+ return -1; // Return an error code to indicate file open failure
+ }
}
-#if HARD_OFF
- int ret = 0;
- ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+#if HARD_OFF
+ int ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
if (ret != 1)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data is error! %d", ret);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
}
#else
- if (fp == NULL)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
- }
Trainingdata_hard hard = {0};
fseek(fp, 0, SEEK_SET);
int ret = fread((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
-
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num);
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fread %d", ret);
if (ret == 0)
{
+ // Write new header if it doesn't exist
hard.num = 1;
hard.st = timeday;
fseek(fp, 0, SEEK_SET);
ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
-
- if (ret != 1)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret);
- }
-
- ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
if (ret != 1)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "1 fwrite_data %d", ret);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
}
}
else
{
- ret = fseek(fp, 8, SEEK_END);
- if (ret != 0)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fseek %d", ret);
- }
- hard.num++;
- ret = fwrite((void *)&(hard), sizeof(Trainingdata_hard), 1, fp);
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num);
- if (ret)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d", ret);
- }
-
fseek(fp, 0, SEEK_END);
- // fclose(fp);
-
- // fp= fopen(file_path,"ab+");
- ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+ hard.num++;
+ ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
if (ret != 1)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "2 fwrite_data %d", ret);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
}
}
+ fseek(fp, 0, SEEK_END);
+ ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
+ if (ret != 1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
+ }
#endif
- fseek(fp, 0, SEEK_SET);
- fclose(fp);
- return 1;
+
+ fclose(fp); // Close the file after writing data
+ return 0; // Return success code
}
// 读数据
Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset)
if (fp == NULL || get_Trainingdata_data_num(fp) < offset)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d", get_Trainingdata_data_num(fp));
+ free(data); // 修复内存泄漏
return NULL;
}
#if HARD_OFF