繁体   English   中英

从路径中提取文件名

[英]Extracting a file name from the path

好吧,我有以下内容:

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”。 我承认在编程方面我还是很环保,对指针了解甚少,而对双指针则了解甚少。 但任何帮助将不胜感激。 谢谢。

您正在使用的函数的编写长度为76行。 那不是灾难性的长,但是到了很难推理的地步。 将这个功能分成几个较小的功能可能是值得的。 您正在处理的一个问题是如何从完整路径提取文件名。 您可以编写一个签名如下的函数:

char *filename_from_path(const char *fullpath);

它采用完整路径并返回仅包含文件名的新字符串。 (注意:您必须注意谁分配和释放文件名字符串)。 分解这样一个函数的整洁之处在于,通常有关于如何做这些小片段的好建议:

例如, 使用MSVS2005从C中的完整路径提取文件名

分离出该逻辑将使您更容易推断正在处理的较大循环。

错误在以下代码中:

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

这里有几个问题:

  1. len不包含空终止符,因此您没有分配足够的内存。 您还需要一个角色。
  2. 您无法再次调用DragQueryFile来填充缓冲区。
  3. 您对filePathList[i][index]首次访问超出了范围,因为index超出了数组的末尾。
  4. 由于未初始化filePathList[i]因此循环可能会递减至index 0,然后产生访问冲突。
  5. 即使修复了所有这些错误,您也需要在循环中测试index>=0 ,以防字符串不包含路径分隔符。

我没有看过您问题中的任何其他代码,但我敢打赌我没有找到所有错误。 现在应该足够了。

Joachim关于使用_splitpath的建议非常合理,但在另一方面,您确实需要掌握这种编码。

您也可以打开文件以找到其名称。 尽管那可能和走这条路一样慢。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM