{"id":512,"date":"2024-12-25T00:54:39","date_gmt":"2024-12-24T16:54:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=512"},"modified":"2024-12-25T00:54:39","modified_gmt":"2024-12-24T16:54:39","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e5%90%8c%e5%b9%b3%e5%8f%b0%e4%b8%8a%e8%8e%b7%e5%8f%96-c-c-%e7%a8%8b%e5%ba%8f%e7%9a%84%e6%89%a7%e8%a1%8c%e7%9b%ae%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e5%90%8c%e5%b9%b3%e5%8f%b0%e4%b8%8a%e8%8e%b7%e5%8f%96-c-c-%e7%a8%8b%e5%ba%8f%e7%9a%84%e6%89%a7%e8%a1%8c%e7%9b%ae%e5%bd%95%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u83b7\u53d6 C\/C++ \u7a0b\u5e8f\u7684\u6267\u884c\u76ee\u5f55\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 C\/C++ \u7a0b\u5e8f\u4e2d\uff0c\u83b7\u53d6\u6267\u884c\u76ee\u5f55\uff08\u5373\u5f53\u524d\u7a0b\u5e8f\u6240\u5728\u7684\u76ee\u5f55\uff09\u7684\u65b9\u6cd5\u4f1a\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u83b7\u53d6\u6267\u884c\u76ee\u5f55\u7684\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u5728 Linux \u548c Unix \u7cfb\u7edf\u4e0a<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Linux \u6216 Unix \u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>readlink<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7a0b\u5e8f\u7684\u6267\u884c\u76ee\u5f55\u3002\u5177\u4f53\u6765\u8bf4\uff0c<code>\/proc\/self\/exe<\/code> \u662f\u4e00\u4e2a\u6307\u5411\u5f53\u524d\u6267\u884c\u7a0b\u5e8f\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u901a\u8fc7\u8bfb\u53d6\u5b83\u53ef\u4ee5\u83b7\u5f97\u7a0b\u5e8f\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;limits.h&gt;\n\nint main() {\n    char path&#91;PATH_MAX];\n    ssize_t len = readlink(\"\/proc\/self\/exe\", path, sizeof(path) - 1);\n\n    if (len == -1) {\n        perror(\"readlink\");\n        return 1;\n    }\n\n    path&#91;len] = '\\0'; \/\/ Null-terminate the string\n    char *dir = dirname(path);  \/\/ Get the directory part of the path\n\n    printf(\"Current execution directory: %s\\n\", dir);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>readlink(\"\/proc\/self\/exe\", ...)<\/code> \u83b7\u53d6\u5f53\u524d\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/li>\n\n\n\n<li><code>dirname(path)<\/code> \u7528\u4e8e\u83b7\u53d6\u8def\u5f84\u7684\u76ee\u5f55\u90e8\u5206\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u5728 Windows \u7cfb\u7edf\u4e0a<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Windows \u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>GetModuleFileName<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7a0b\u5e8f\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u83b7\u53d6\u76ee\u5f55\u90e8\u5206\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;windows.h&gt;\n#include &lt;string.h&gt;\n\nint main() {\n    char path&#91;MAX_PATH];\n    DWORD length = GetModuleFileName(NULL, path, MAX_PATH);\n\n    if (length == 0) {\n        printf(\"Error getting path\\n\");\n        return 1;\n    }\n\n    \/\/ Find the last backslash in the path and replace it with a null character\n    char *dir = strrchr(path, '\\\\');\n    if (dir) {\n        *dir = '\\0';  \/\/ Null-terminate the path at the last backslash\n    }\n\n    printf(\"Current execution directory: %s\\n\", path);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>GetModuleFileName(NULL, ...)<\/code> \u83b7\u53d6\u5f53\u524d\u6267\u884c\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/li>\n\n\n\n<li><code>strrchr(path, '\\\\')<\/code> \u7528\u4e8e\u67e5\u627e\u8def\u5f84\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u53cd\u659c\u6760\uff08<code>\\<\/code>\uff09\uff0c\u7136\u540e\u66ff\u6362\u5b83\u4e3a\u5b57\u7b26\u4e32\u7684\u7ec8\u6b62\u7b26\uff08<code>\\0<\/code>\uff09\u6765\u83b7\u53d6\u76ee\u5f55\u90e8\u5206\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u5728 macOS \u548c Linux \u4e0a\u4f7f\u7528 <code>realpath<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 macOS \u548c Linux \u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>realpath<\/code> \u51fd\u6570\u83b7\u53d6\u7a0b\u5e8f\u7684\u5b9e\u9645\u8def\u5f84\uff0c\u8fd9\u5bf9\u4e8e\u5904\u7406\u7b26\u53f7\u94fe\u63a5\u7279\u522b\u6709\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    char path&#91;PATH_MAX];\n    if (realpath(\"\/proc\/self\/exe\", path) == NULL) {\n        perror(\"realpath\");\n        return 1;\n    }\n\n    char *dir = dirname(path);  \/\/ Get the directory part of the path\n\n    printf(\"Current execution directory: %s\\n\", dir);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>realpath(\"\/proc\/self\/exe\", ...)<\/code> \u83b7\u53d6\u7a0b\u5e8f\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528\u6807\u51c6\u5e93\uff08\u8de8\u5e73\u53f0\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>std::filesystem<\/code>\uff08\u4ece C++17 \u5f00\u59cb\uff09\u6765\u83b7\u53d6\u5f53\u524d\u6267\u884c\u76ee\u5f55\u3002\u5b83\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;filesystem&gt;\n\nint main() {\n    std::filesystem::path exePath = std::filesystem::current_path(); \/\/ \u83b7\u53d6\u5f53\u524d\u8def\u5f84\n    std::cout &lt;&lt; \"Current execution directory: \" &lt;&lt; exePath &lt;&lt; std::endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>std::filesystem::current_path()<\/code> \u8fd4\u56de\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u5b83\u4f1a\u968f\u7740\u7a0b\u5e8f\u6267\u884c\u7684\u4e0d\u540c\u800c\u53d8\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5e73\u53f0<\/th><th>\u83b7\u53d6\u65b9\u6cd5<\/th><\/tr><\/thead><tbody><tr><td><strong>Linux\/Unix<\/strong><\/td><td>\u4f7f\u7528 <code>readlink(\"\/proc\/self\/exe\")<\/code> \u83b7\u53d6\u8def\u5f84<\/td><\/tr><tr><td><strong>Windows<\/strong><\/td><td>\u4f7f\u7528 <code>GetModuleFileName<\/code> \u83b7\u53d6\u8def\u5f84<\/td><\/tr><tr><td><strong>macOS\/Linux<\/strong><\/td><td>\u4f7f\u7528 <code>realpath(\"\/proc\/self\/exe\")<\/code> \u83b7\u53d6\u8def\u5f84<\/td><\/tr><tr><td><strong>\u8de8\u5e73\u53f0<\/strong><\/td><td>\u4f7f\u7528 C++17 \u7684 <code>std::filesystem::current_path()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u6839\u636e\u6240\u4f7f\u7528\u7684\u5e73\u53f0\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u7a0b\u5e8f\u7684\u6267\u884c\u76ee\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C\/C++ \u7a0b\u5e8f\u4e2d\uff0c\u83b7\u53d6\u6267\u884c\u76ee\u5f55\uff08\u5373\u5f53\u524d\u7a0b\u5e8f\u6240\u5728\u7684\u76ee\u5f55\uff09\u7684\u65b9\u6cd5\u4f1a\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u83b7\u53d6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=512"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions"}],"predecessor-version":[{"id":513,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/512\/revisions\/513"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}