{"id":37,"date":"2021-09-13T23:02:32","date_gmt":"2021-09-14T07:02:32","guid":{"rendered":"https:\/\/www.qiuqiuren.club\/?p=37"},"modified":"2024-08-02T21:12:24","modified_gmt":"2024-08-03T05:12:24","slug":"stdfilesystemrelative-%e5%bf%85%e9%a1%bb%e4%bc%a0%e5%85%a5%e5%b7%b2%e5%ad%98%e5%9c%a8%e7%9a%84%e6%96%87%e4%bb%b6%e7%b3%bb%e7%bb%9f%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/www.qiuqiuren.club\/?p=37","title":{"rendered":"std::filesystem::relative\u548cstd::filesystem::path::lexically_relative"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u5f00\u53d1\u7684\u4e00\u4e2a\u5728\u8fdc\u7a0b\u4e3b\u673a\u548c\u672c\u5730\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u6587\u4ef6\u7684\u5de5\u5177\uff0c\u5728\u66f4\u65b0\u540e\u6709\u4e9b\u7528\u6237\u53cd\u5e94\u4f7f\u7528\u65f6\u4f1a\u5d29\u6e83\uff0c\u800c\u591a\u6570\u7528\u6237\u53c8\u4e0d\u5d29\u6e83\u3002<\/p>\n<h2>\u5206\u6790\u539f\u56e0<\/h2>\n<p>\u8ba9\u5d29\u6e83\u7684\u5ba2\u6237\u6302\u4e0ax64dbg\uff0c\u518d\u89e6\u53d1\u5d29\u6e83\uff0c\u5f97\u5230\u4e86\u5d29\u6e83\u70b9\u548c\u8c03\u7528\u6808\u3002\u56de\u53bbida\u770b\uff0c\u53d1\u73b0\u5d29\u6e83\u5728\u8fd9\u91cc<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/09\/Pasted-11.png\" alt=\"\" \/><\/p>\n<p>\u5bf9\u5e94\u5230\u6e90\u7801\uff08\u51e0\u884c\u6e90\u7801\u7f16\u8bd1\u6210\u673a\u5668\u7801\u5c45\u7136\u81a8\u80c0\u6210\u8fd9\u4e2a\u6837\u5b50\uff0c\u8fd9\u8981\u662f\u6ca1\u7b26\u53f7\u6587\u4ef6\u90a3\u53ef\u96be\u5206\u6790\u4e86\uff09<\/p>\n<pre><code class=\"language-c\">std::vector&lt;BYTE&gt; ClipboardFileDataObject::ChangeGroupDescriptorToRelative(const std::vector&lt;BYTE&gt;&amp; groupDescriptor)\n{\n    std::vector&lt;BYTE&gt; ret = groupDescriptor;\n\n    FILEGROUPDESCRIPTORW* fgd = (FILEGROUPDESCRIPTORW*)&amp;ret[0];\n\n    std::filesystem::path firstFile = fgd-&gt;fgd[0].cFileName;\n    std::filesystem::path root = firstFile.parent_path();\n\n    FILEDESCRIPTORW* fd = fgd-&gt;fgd;\n    for (int i = 0; i &lt; fgd-&gt;cItems; i++)\n    {\n        std::filesystem::path currentPath = fd[i].cFileName;\n        std::filesystem::path relativePath = std::filesystem::relative(currentPath, root);\n        wcscpy_s(fd[i].cFileName, relativePath.wstring().c_str());\n    }\n\n    return ret;\n}<\/code><\/pre>\n<p>\u76f4\u89c2\u4e0a\u6765\u770b\uff0c\u662fstd::filesystem::relative()\u629b\u4e86\u5f02\u5e38\u3002\u53ef\u662f\u8fd9\u51ed\u5565\u629b\u5f02\u5e38\uff0c\u6211\u53ea\u662f\u60f3\u7b80\u5355\u8ba1\u7b97\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u5230\u53e6\u4e00\u4e2a\u8def\u5f84\u7684\u76f8\u5bf9\u5730\u5740\u800c\u5df2\u3002<\/p>\n<h2>\u6d4b\u8bd5<\/h2>\n<p>\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u7a0b\u5e8f\uff0c\u5728\u6211\u548c\u5d29\u6e83\u5ba2\u6237\u7684\u673a\u5668\u4e0a\uff0c\u8dd1\u7684\u7ed3\u679c\u4e0d\u4e00\u6837\u3002<\/p>\n<pre><code>int main()\n{\n    std::cout &lt;&lt; &quot;Hello World!\\n&quot;;\n\n    const char* filePath = &quot;D:\\\\Minmus\\\\Clipboard\\\\password123.zip&quot;;\n\n    try\n    {\n        std::filesystem::path path = filePath;\n        std::filesystem::path root = path.root_path();\n        std::filesystem::path relPath = std::filesystem::relative(path, root);\n        std::cout &lt;&lt; relPath.string() &lt;&lt; std::endl;\n\n    } \n    catch (std::exception e)\n    {\n        std::cout &lt;&lt; e.what() &lt;&lt; std::endl;\n    }\n\n    return 0;\n}<\/code><\/pre>\n<p>\u5728\u6211\u673a\u5668\u4e0a\u4e0d\u629b\u5f02\u5e38\uff0c\u5ba2\u6237\u7684\u673a\u5668\u4e0a\u629b\u5f02\u5e38\u3002\u7136\u540e\u53bb\u641c\u4e86\u4e0bfilesystem\u7684\u6587\u6863\uff0c\u53d1\u73b0\u8fd9\u4e2astd::filesystem::relative()\u51fd\u6570\u5c45\u7136\u8981\u6c42\u4f20\u5165\u7684\u8def\u5f84\u662f\u5df2\u5b58\u5728\u7684\uff0c\u5988\u8036\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f80relative()\u51fd\u6570\u4f20\u4e0d\u5b58\u5728\u7684\u8def\u5f84\uff0c\u540e\u679c\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u53ef\u4ee5\u6210\u529f\uff0c\u53ef\u4ee5\u4e0d\u6210\u529f\u3002<\/p>\n<p>\u53c8\u8fdb\u4e00\u6b65\u6d4b\u8bd5\uff0c\u53d1\u73b0\u5927\u591a\u6570\u60c5\u51b5\uff0c\u5373\u4f7f\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e5f\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u4f46\u662f\u5982\u679c\u8def\u5f84\u7684\u76d8\u7b26\u6070\u597d\u548c\u7cfb\u7edf\u4e2d\u5149\u9a71\u7684\u76d8\u7b26\u4e00\u81f4\uff0c\u5b83\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38:<code>weakly_canonical: \u8bbe\u5907\u672a\u5c31\u7eea<\/code>\u3002\u800c\u6211\u90a3\u4e2a\u7528\u6237\u662f\u4e2a\u865a\u62df\u673a\u7528\u6237\uff0cd\u76d8\u5c31\u662f\u5149\u9a71\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u8fd9\u4e2a\u51fd\u6570boom\u4e86\u3002<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>std::filesystem::relative()\u5fc5\u987b\u4fdd\u8bc1\u4f20\u5165\u7684\u8def\u5f84\u5728\u672c\u673a\u5df2\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u5f02\u5e38\u884c\u4e3a\u3002\u56e0\u4e3a\u5b83\u9700\u8981\u5904\u7406\u7b26\u53f7\u8fde\u63a5\u7b49\u5185\u5bb9\u3002\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n<blockquote>\n<p>Returns p made relative to base. Resolves symlinks and normalizes both p and base before other processing. Effectively returns weakly_canonical(p).lexically_relative(weakly_canonical(base)) or weakly_canonical(p, ec).lexically_relative(weakly_canonical(base, ec)), except the error code form returns path() at the first error occurrence, if any<\/p>\n<p>Parameters<br \/>\np &#8211;   an existing path<br \/>\nbase  &#8211;   base path, against which p will be made relative\/proximate<br \/>\nec    &#8211;   error code to store error status to<\/p>\n<\/blockquote>\n<p>\u5176\u5b9e\u6587\u6863\u91cc\u9762\u4e5f\u8bf4\u4e86\uff0c\u5982\u679c\u53ea\u662f\u9700\u8981\u83b7\u5f97<strong>\u8bed\u4e49\u4e0a<\/strong>\u7684\u76f8\u5bf9\u8def\u5f84\uff0c\u4f7f\u7528lexically_relative()\u5373\u53ef\u3002<\/p>\n<blockquote>\n<p>These conversions are purely lexical. They do not check that the paths exist, do not follow symlinks, and do not access the filesystem at all. For symlink-following counterparts of lexically_relative and lexically_proximate, see relative and proximate.<\/p>\n<\/blockquote>\n<p>\u770b\u6765\u8fd8\u662fcpp\u5b98\u65b9\u8003\u8651\u7684\u5468\u5168\u3002<\/p>\n<h2>\u4fee\u590d<\/h2>\n<p>\u4f7f\u7528std::filesystem::path::lexically_relative()\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u5f00\u53d1\u7684\u4e00\u4e2a\u5728\u8fdc\u7a0b\u4e3b\u673a\u548c\u672c\u5730\u76f4\u63a5\u590d\u5236\u7c98\u8d34\u6587\u4ef6\u7684\u5de5\u5177\uff0c\u5728\u66f4\u65b0\u540e\u6709\u4e9b\u7528\u6237\u53cd\u5e94\u4f7f\u7528\u65f6\u4f1a\u5d29\u6e83\uff0c\u800c\u591a\u6570\u7528\u6237\u53c8\u4e0d\u5d29\u6e83\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/37"}],"collection":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":60,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/60"}],"wp:attachment":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}