加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – WIN32_FIND_DATA – 获取绝对路径

发布时间:2020-12-16 07:51:26 所属栏目:百科 来源:网络整理
导读:我正在使用这样的东西: std::string tempDirectory = "./test/*";WIN32_FIND_DATA directoryHandle;memset(directoryHandle,sizeof(WIN32_FIND_DATA));//perhaps redundant???std::wstring wideString = std::wstring(tempDirectory.begin(),tempDirectory.
我正在使用这样的东西:
std::string tempDirectory = "./test/*";

WIN32_FIND_DATA directoryHandle;
memset(&directoryHandle,sizeof(WIN32_FIND_DATA));//perhaps redundant???

std::wstring wideString = std::wstring(tempDirectory.begin(),tempDirectory.end());
LPCWSTR directoryPath = wideString.c_str();

//iterate over all files
HANDLE handle = FindFirstFile(directoryPath,&directoryHandle);
while(INVALID_HANDLE_VALUE != handle)
{
    //skip non-files
    if (!(directoryHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
        //convert from WCHAR to std::string
        size_t size = wcslen(directoryHandle.cFileName);
        char * buffer = new char [2 * size + 2];
        wcstombs(buffer,directoryHandle.cFileName,2 * size + 2);
        std::string file(buffer);
        delete [] buffer;

        std::cout << file;
    }

    if(FALSE == FindNextFile(handle,&directoryHandle)) break;
}

//close the handle
FindClose(handle);

它在相对目录./test/*中打印每个文件的名称.

有没有办法确定这个目录的绝对路径,就像Linux上的realpath()一样,不涉及像BOOST这样的第三方库?我想打印每个文件的绝对路径.

解决方法

请参阅 GetFullPathName功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读