0
從以前的問題,下面就我在這裏:複製從指向字符串的指針字符串轉換成動態數組
Copying a string from a pointer to a string
我現在正在試圖複製的字符串添加到一個動態數組,這將根據SD卡上文件的數量逐漸增加,一旦卡被換出,它將被刪除。
編輯:此代碼第一次正常工作。 SD卡的內容更改後,調用reReadSD()函數並釋放fileList。讀取SD卡的新內容並將新值寫入fileList,但是,在打印出fileList中的名稱時,我得到的是符號而不是專用名稱。我認爲這是釋放fileList並將其重新初始化的錯誤,因爲同一代碼塊在系統啓動時(第一次調用reReadSD時)會起作用。任何人都可以對此有所瞭解嗎?
編輯:更新的代碼
void reReadSD()
{
free(fileList);
files_allocated=0;
num_files=0;
reRead_flag=0;
if(f_mount(0, &fatfs) != FR_OK){
/* efs initialisation fails*/
}//end f_mount
FRESULT result;
char *path = '/'; //look in root of sd card
result = f_opendir(&directory, path); //open directory
if(result==FR_OK){
for(;;){
result = f_readdir(&directory, &fileInfo); //read directory
if(result==FR_OK){
if(fileInfo.fname[0]==0){break;} //end of dir reached escape for(;;)
if(fileInfo.fname[0]=='.'){continue;} //ignore '.' files
TCHAR* temp;
temp = malloc(strlen(fileInfo.fname)+1);
strcpy(temp, fileInfo.fname);
AddToArray(temp);
}//end read_dir result==fr_ok
}//end for(;;)
}//end open_dir result==fr_ok
}//end reReadSD
和..
void AddToArray (TCHAR* item)
{
u32 delay;
if(num_files == files_allocated)
{
if (files_allocated==0)
files_allocated=5; //initial allocation
else
files_allocated+=5; //more space needed
//reallocate with temp variable
void *_tmp = realloc(fileList, (files_allocated * sizeof(TCHAR*)));
//reallocation error
if (!_tmp)
{
LCD_ErrLog("Couldn't realloc memory!\n");
return;
}
// Things are looking good so far
fileList = _tmp;
}//end num_files==files_allocated
fileList[num_files] = item;
num_files++;
}//end AddToArray
凡
TCHAR **fileList;
u32 num_files=0;
u32 files_allocated=0;
非常感謝,我沒有發現。同意第二點,這個想法是以塊的形式分配mem if(files_allocated == 0) files_allocated = 5; //初始分配 else files_allocated + = 5; //需要更多空間 – David