好的,我有以下内容:
wchar_t **filePathList;
这包含正在添加到列表框中的文件列表。问题是它们显示了整个文件路径,而我只想得到名称。我考虑过使用:
wchar_t *tempChar;
从 filePathList 的末尾开始,然后返回直到我到达\
. 问题是我不确定如何管理这个。这是我到目前为止的代码:
afx_msg void Send::OnDropFiles(HDROP hDropInfo)
{
if(uploadInProgress)
{
MessageBox(L"Please wait for current upload to finish before adding files", L"Upload in progress", MB_OK);
return;
}
int len;
int prevNFiles = nFiles;
wchar_t **buffer = filePathList;
wchar_t *tempChar = NULL;
// get number of files dropped into window
nFiles += DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
filePathList = (wchar_t**)malloc(nFiles*sizeof(wchar_t*));
if(!filePathList)
{
MessageBox(L"FilePath list memory allocation failed", L"Error");
nFiles = 0;
if(buffer)
{
for(int i=0; i<prevNFiles; i++)
{
if(buffer[i]) free(buffer[i]);
}
free(buffer);
}
return;
}
memset(filePathList, 0, nFiles*sizeof(wchar_t*));
// get file names
for(int i=0; i<nFiles; i++)
{
if(i < prevNFiles)
{ // previously entered files
filePathList[i] = buffer[i];
continue;
}
// newly dropped files
len = DragQueryFile(hDropInfo, i-prevNFiles, NULL, 0)+1; // 1 for \0
tempChar = (wchar_t*)malloc(len*sizeof(wchar_t));
filePathList[i] = (wchar_t*)malloc(len*sizeof(wchar_t));
int index = len;
// Attempting to iterate through the path to get the file name
while(filePathList[i][index] != '\\')
{
tempChar = filePathList[index];
index--;
}
filePathList[i] = tempChar;
if(!filePathList[i])
{
MessageBox(L"FilePath memory allocation failed", L"Error");
for(int j=0; j<i; j++)
{
if(filePathList[j]) free(filePathList[j]);
}
free(filePathList); filePathList = NULL;
nFiles = 0;
break;
}
len = DragQueryFile(hDropInfo, i-prevNFiles, filePathList[i], len);
}
if(buffer) free(buffer);
// display files
UpdateFileListDisplay();
}
问题是 Visual Studio 将 tempChar 报告为“坏 ptr”。我承认我在编程方面仍然很年轻,对指针知之甚少,更不用说双指针了。但任何帮助将不胜感激。谢谢你。