简体   繁体   English

C - 为什么将一个元素分配给动态分配数组的第一个元素总是什么都不返回

[英]C - Why does assigning an element to a first element of adynamically allocated array always return nothing

I am developing an application where I need to read some files off an SD Card.我正在开发一个应用程序,我需要从 SD 卡中读取一些文件。 Everything runs on an STM32 microcontroller with FatFS.一切都在带有 FatFS 的 STM32 微控制器上运行。 I just open the root, and iterate over each element in that directory and check whether it's a file or a folder, I save the information how many files I have.我只是打开根目录,然后遍历该目录中的每个元素并检查它是文件还是文件夹,我保存了我有多少文件的信息。 Then I allocate given amout of memory for said files (no of files x buffer for the name).然后我为所述文件分配给定数量的 memory (名称的文件数 x 缓冲区)。 Next, I read the directory again, like in the first step.接下来,我再次读取目录,就像在第一步中一样。 This time I copy the name of each file to the aforementioned place in memory.这次我把每个文件的名字复制到前面提到的memory的地方。 My problem is, everytime I copy anything to the 2D array, the firs element contains literally nothing, and file names start from the second index.我的问题是,每次我将任何内容复制到二维数组时,第一个元素实际上什么都不包含,并且文件名从第二个索引开始。 What am I doing wrong?我究竟做错了什么?

//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;
}

Then if I do: lcd_str_XY(0,1, file_names_array[0]);然后如果我这样做: lcd_str_XY(0,1, file_names_array[0]); There is nothing in this place in memory lcd_str_XY(0,1, file_names_array[1]); memory lcd_str_XY(0,1, file_names_array[1]);这个地方什么都没有Prints the first file's name打印第一个文件的名称

files_get_names doesn't take into account anymore that f_opendir also returns directory. files_get_names不再考虑f_opendir也返回目录。 In fact, from your description it is likely that it returns a directory as the first entry.实际上,根据您的描述,它很可能会返回一个目录作为第一个条目。 Yet files_get_names still increases the index and goes on.然而files_get_names仍然增加索引并继续。

Fix it like so:像这样修复它:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将元素分配给未调整大小的数组会更改 c 中分配的元素? - Why assigning an element to unsized array change the assigned element in c? 为什么字符串数组只能返回第一个元素,而不能返回后续元素? - Why String Array only get to return first element, but not the subsequent element? C:向动态分配的数组添加元素 - C: adding element to dynamically allocated array 为什么将\ 0定义为C中char数组的第一个元素? - Why define \0 as the first element of a char array in C? 为什么数组保存C中第一个元素的地址? - Why array holds the address of the first element in C? 为什么解除指向一个整数数组(在2d数组中)的指针返回(或衰减到)指向第一个元素的指针? - why does derefrencing a pointer to an array of integers(in 2d array) return(or decay to) pointer to first element? 数组/ arrayname不是指向C中第一个元素的指针吗? - Isn't an array/arrayname always a pointer to the first element in C? C-如果元素的索引大于动态分配的数组大小,它将返回null吗? - C- If index of element bigger than dynamically allocated array size, will it return null? C-为什么 Bubble Sort of Struct 数组跳过数组中的第一个元素? - C-Why does Bubble Sort of Struct array skip the first element in the Array? 为什么使用a [i] [j]访问动态分配的2D数组元素会起作用? - Why does it work to access a dynamically allocated 2D array element with a[i][j]?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM