在未知数量的文件中查找最新文件的程序
Program to find the newest file among unknown number of files
我需要用 C 和 FATFS 为 STM32 MCU 编写程序,以在未知数量的文件中找到最新的文件。文件名包含其创建日期,文件名中的数字以“_”分隔。例如:oil_sensor_22_07_20_13_15.csv
我写了一段代码来从文件中提取日期和时间并计算它们的时差。但是我不知道如何在所有文件中找到最新的文件。
我包括计算两个文件之间的时间差的代码和在两个文件中找到最新文件的代码。
计算两个文件名时间差的函数:
double calc_passed_secs(char * name_str_01, char * name_str_02 ){
struct tm tm_struct_1,tm_struct_2;
time_t time_t_1,time_t_2;
// Returns first token
char* token = strtok(name_str_01, "_");
token = strtok(NULL, "_");
token = strtok(NULL, "_");
tm_struct_1.tm_mday = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_mon = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_year = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_hour = atoi(token);
token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_1.tm_min = atoi(token);
tm_struct_1.tm_sec = 0;
// Returns first token
token = strtok(name_str_02, "_");
token = strtok(NULL, "_");
token = strtok(NULL, "_");
tm_struct_2.tm_mday = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_mon = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_year = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_hour = atoi(token);
token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_2.tm_min = atoi(token);
tm_struct_2.tm_sec = 0;
double seconds = difftime(mktime(&tm_struct_1),mktime(&tm_struct_2));
printf("\r\nTime difference in seconds: %.f\r\n",seconds);
return seconds;
}
和查找最新文件的程序(该程序未给出所需的结果)。
char newest_log_file[128];
char oldest_log_file[128];
char first_log_file [128];
char second_log_file [128];
char first_log_file_cpy [128];
char second_log_file_cpy [128];
//char printf_buff [128];
// find first file
fr = f_findfirst(&dj, &fno1, "", "oil_sensor_*.csv");
strcpy( first_log_file,fno1.fname);
strcpy(first_log_file_cpy, first_log_file);
if (!fno1.fname[0]) {
bool make_first_log_file = true;
}
if (fno1.fname[0]) {
fr = f_findnext(&dj, &fno1);
}
if (fno1.fname[0]) {
strcpy(second_log_file,fno1.fname);
strcpy(second_log_file_cpy, second_log_file);
}
printf("\r\nFirst Log File: %s\r\n", first_log_file);
printf("\r\nSecond Log File: %s\r\n", second_log_file);
double seconds = calc_passed_secs(first_log_file , second_log_file);
if (seconds < 0){
strcpy(newest_log_file, second_log_file_cpy);
}
if (seconds > 0) {
strcpy(newest_log_file, first_log_file_cpy);
}
printf("\r\nnewest file: %s\r\n", newest_log_file);
do {
f_findnext(&dj,&fno1);
strcpy(first_log_file, fno1.fname);
strcpy(first_log_file_cpy, first_log_file);
f_findnext(&dj,&fno1);
strcpy(second_log_file,fno1.fname);
strcpy(second_log_file_cpy, second_log_file);
printf("\r\nFirst Log File: %s\r\n", first_log_file);
printf("\r\nSecond Log File: %s\r\n", second_log_file);
if (seconds < 0){
strcpy(newest_log_file, second_log_file_cpy);
}
if (seconds > 0) {
strcpy(newest_log_file, first_log_file_cpy);
}
printf("\r\nnewest file: %s\r\n", newest_log_file);
seconds = calc_passed_secs(first_log_file, second_log_file);
} while (fr == FR_OK && fno1.fname[0]);
f_closedir(&dj);
算法很简单,伪代码如下:
newest_file = first_file
for file in file_list:
if file.date > newest_file.date:
newest_file = file
它所做的只是在循环发生之前将第一个文件存储在文件列表中,然后您遍历所有文件并将它们的日期与存储的日期进行比较。如果它较新,则将存储的文件更改为刚刚检查过的文件,然后继续循环。完成循环后,引用将指向列表中的最新文件。
我需要用 C 和 FATFS 为 STM32 MCU 编写程序,以在未知数量的文件中找到最新的文件。文件名包含其创建日期,文件名中的数字以“_”分隔。例如:oil_sensor_22_07_20_13_15.csv
我写了一段代码来从文件中提取日期和时间并计算它们的时差。但是我不知道如何在所有文件中找到最新的文件。
我包括计算两个文件之间的时间差的代码和在两个文件中找到最新文件的代码。
计算两个文件名时间差的函数:
double calc_passed_secs(char * name_str_01, char * name_str_02 ){
struct tm tm_struct_1,tm_struct_2;
time_t time_t_1,time_t_2;
// Returns first token
char* token = strtok(name_str_01, "_");
token = strtok(NULL, "_");
token = strtok(NULL, "_");
tm_struct_1.tm_mday = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_mon = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_year = atoi(token);
token = strtok(NULL, "_");
tm_struct_1.tm_hour = atoi(token);
token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_1.tm_min = atoi(token);
tm_struct_1.tm_sec = 0;
// Returns first token
token = strtok(name_str_02, "_");
token = strtok(NULL, "_");
token = strtok(NULL, "_");
tm_struct_2.tm_mday = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_mon = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_year = atoi(token);
token = strtok(NULL, "_");
tm_struct_2.tm_hour = atoi(token);
token = strtok(NULL, "_");
token = strtok(token, ".");
tm_struct_2.tm_min = atoi(token);
tm_struct_2.tm_sec = 0;
double seconds = difftime(mktime(&tm_struct_1),mktime(&tm_struct_2));
printf("\r\nTime difference in seconds: %.f\r\n",seconds);
return seconds;
}
和查找最新文件的程序(该程序未给出所需的结果)。
char newest_log_file[128];
char oldest_log_file[128];
char first_log_file [128];
char second_log_file [128];
char first_log_file_cpy [128];
char second_log_file_cpy [128];
//char printf_buff [128];
// find first file
fr = f_findfirst(&dj, &fno1, "", "oil_sensor_*.csv");
strcpy( first_log_file,fno1.fname);
strcpy(first_log_file_cpy, first_log_file);
if (!fno1.fname[0]) {
bool make_first_log_file = true;
}
if (fno1.fname[0]) {
fr = f_findnext(&dj, &fno1);
}
if (fno1.fname[0]) {
strcpy(second_log_file,fno1.fname);
strcpy(second_log_file_cpy, second_log_file);
}
printf("\r\nFirst Log File: %s\r\n", first_log_file);
printf("\r\nSecond Log File: %s\r\n", second_log_file);
double seconds = calc_passed_secs(first_log_file , second_log_file);
if (seconds < 0){
strcpy(newest_log_file, second_log_file_cpy);
}
if (seconds > 0) {
strcpy(newest_log_file, first_log_file_cpy);
}
printf("\r\nnewest file: %s\r\n", newest_log_file);
do {
f_findnext(&dj,&fno1);
strcpy(first_log_file, fno1.fname);
strcpy(first_log_file_cpy, first_log_file);
f_findnext(&dj,&fno1);
strcpy(second_log_file,fno1.fname);
strcpy(second_log_file_cpy, second_log_file);
printf("\r\nFirst Log File: %s\r\n", first_log_file);
printf("\r\nSecond Log File: %s\r\n", second_log_file);
if (seconds < 0){
strcpy(newest_log_file, second_log_file_cpy);
}
if (seconds > 0) {
strcpy(newest_log_file, first_log_file_cpy);
}
printf("\r\nnewest file: %s\r\n", newest_log_file);
seconds = calc_passed_secs(first_log_file, second_log_file);
} while (fr == FR_OK && fno1.fname[0]);
f_closedir(&dj);
算法很简单,伪代码如下:
newest_file = first_file
for file in file_list:
if file.date > newest_file.date:
newest_file = file
它所做的只是在循环发生之前将第一个文件存储在文件列表中,然后您遍历所有文件并将它们的日期与存储的日期进行比较。如果它较新,则将存储的文件更改为刚刚检查过的文件,然后继续循环。完成循环后,引用将指向列表中的最新文件。