0

我正在开发一个应用程序,我需要从 SD 卡中读取一些文件。一切都在带有 FatFS 的 STM32 微控制器上运行。我只是打开根目录,然后遍历该目录中的每个元素并检查它是文件还是文件夹,我保存了我有多少文件的信息。然后我为所述文件分配给定的内存量(名称的文件数x缓冲区)。接下来,我再次读取目录,就像在第一步中一样。这次我将每个文件的名称复制到内存中的上述位置。我的问题是,每次我将任何内容复制到二维数组时,第一个元素实际上什么都不包含,并且文件名从第二个索引开始。我究竟做错了什么?

//Returns 0 on error or no files
uint8_t files_get_card_file_count(void)
{
    auto uint8_t file_count = 0;
    auto FRESULT result;
    auto DIR directory;
    auto FILINFO info;

    //Read card contents
    if(f_opendir(&directory, "/")) return 0;

    //Check if there is a card mounted and functioning
    if(disk_status(0) == (STA_NOINIT || STA_NODISK)) return 0;

    for (;;)
    {
        result = f_readdir(&directory,&info);              // Read a directory item
        if (result != FR_OK || info.fname[0] == 0) break;  // Break on error or end of dir
        if (!(info.fattrib & AM_DIR)) ++file_count;
    }
    return file_count;
}
//Make sure we dont allocate more than MAX_FILE_COUNT blocks
char** files_mem_allocate(uint8_t file_count)
{
    auto uint8_t i;

    char **array;
    array = calloc(file_count, sizeof(char *));

    if(array == NULL) return 0;

    for(i=0; i < file_count; i++)
    {
        array[i] = calloc(MAX_FILENAME_LENGTH, sizeof(char));
        if(array[i] == NULL) return 0;
    }

    return array;
}
uint8_t files_get_names(char** array, uint8_t file_no)
{
    auto uint8_t i;
    auto FRESULT result;
    auto DIR directory;
    auto FILINFO info;

    //Read card contents
    if(f_opendir(&directory, "/")) return 1;

    //Check if there is a card mounted and functioning
    if(disk_status(0) == (STA_NOINIT || STA_NODISK)) return 1;

    for(i=0;i<file_no;i++)
    {
        result = f_readdir(&directory,&info);              //Read a directory item
        if (result != FR_OK || info.fname[0] == 0) break;  // Break on error or end of dir
        if (!(info.fattrib & AM_DIR))
        {
            //It is a file.
            //Mind the string safety?
            //Check bounds!
            strcpy(array[i], info.fname);
        }
    }
    return 0;
}

然后如果我这样做: lcd_str_XY(0,1, file_names_array[0]); 内存中这个地方没有任何东西 lcd_str_XY(0,1, file_names_array[1]); 打印第一个文件的名称

4

1 回答 1

1

files_get_names不再考虑f_opendir也返回目录。实际上,根据您的描述,它很可能会返回一个目录作为第一个条目。然而files_get_names仍然增加索引并继续。

像这样修复它:

uint8_t files_get_names(char** array, uint8_t file_no)
{
    auto uint8_t i;
    auto FRESULT result;
    auto DIR directory;
    auto FILINFO info;

    //Read card contents
    if(f_opendir(&directory, "/")) return 1;

    //Check if there is a card mounted and functioning
    if(disk_status(0) == (STA_NOINIT || STA_NODISK)) return 1;

    i = 0;
    for(;;)
    {
        result = f_readdir(&directory,&info);              //Read a directory item
        if (result != FR_OK || info.fname[0] == 0) break;  // Break on error or end of dir
        if (!(info.fattrib & AM_DIR))
        {
            //It is a file.
            //Mind the string safety?
            //Check bounds!
            strcpy(array[i], info.fname);
            i++;
        }
    }
    return 0;
}
于 2021-03-04T07:59:41.957 回答