[英]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.