{"id":454,"date":"2022-11-19T13:32:30","date_gmt":"2022-11-19T05:32:30","guid":{"rendered":"http:\/\/43.143.214.136\/?p=454"},"modified":"2022-11-19T16:09:48","modified_gmt":"2022-11-19T08:09:48","slug":"c%e8%af%ad%e8%a8%80%e9%81%8d%e5%8e%86%e6%96%87%e4%bb%b6%e5%a4%b9%e5%b9%b6%e6%89%b9%e9%87%8f%e6%9b%b4%e6%94%b9%e6%96%87%e4%bb%b6%e5%90%8d","status":"publish","type":"post","link":"https:\/\/lolife.top\/?p=454","title":{"rendered":"C\u8bed\u8a00\u904d\u5386\u6587\u4ef6\u5939\u5e76\u6279\u91cf\u66f4\u6539\u6587\u4ef6\u540d"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;cstring&gt;\n#include &lt;io.h&gt;\n#include&lt;stdio.h&gt; \n#include&lt;string&gt;\nusing namespace std;\n\nvoid listFiles(const char* dir);\nint main()\n{\n\tchar dir&#91;200] = \"D:\\\\data\";\/\/\u6587\u4ef6\u5939\u7684\u76ee\u5f55\n\tlistFiles(dir);\n\treturn 0;\n}\nvoid listFiles(const char* dir)\n{\n\tchar dirNew&#91;200];\n\tstrcpy(dirNew, dir);\n\tstrcat(dirNew, \"\\\\*.*\");    \/\/ \u5728\u76ee\u5f55\u540e\u9762\u52a0\u4e0a\"\\\\*.*\"\u8fdb\u884c\u7b2c\u4e00\u6b21\u641c\u7d22\n\tintptr_t handle;\n\t_finddata_t findData;\n\thandle = _findfirst(dirNew, &amp;findData);\n\tif (handle == -1)        \/\/ \u68c0\u67e5\u662f\u5426\u6210\u529f\n\t\treturn;\n\tdo\n\t{\n\t\tif (findData.attrib &amp; _A_SUBDIR)\/\/\u5982\u679c\u662f\u5b50\u76ee\u5f55\uff08\u6587\u4ef6\u5939\uff09\n\t\t{\n\t\t\tif (strcmp(findData.name, \".\") == 0 || strcmp(findData.name, \"..\") == 0)\/\/\".\"(\u5f53\u524d\u76ee\u5f55)\uff0c\"..\"(\u4e0a\u4e00\u5c42\u76ee\u5f55)\n\t\t\t\tcontinue;\n\t\t\t\/\/\u5982\u679c\u8981\u904d\u5386\u5305\u542b\u5b50\u76ee\u5f55\u5728\u5185\u4e0b\u7684\u6240\u6709\u7684\u6587\u4ef6\uff0c\u5219\u8981\u7ee7\u7eed\u904d\u5386\u5b50\u76ee\u5f55\uff1a\n\t\t\t\/\/strcpy(dirNew, dir);\n\t\t\t\/\/strcat(dirNew, \"\\\\\");\/\/ \u5728\u76ee\u5f55\u540e\u9762\u52a0\u4e0a\"\\\\\"\u548c\u641c\u7d22\u5230\u7684\u76ee\u5f55\u540d\u8fdb\u884c\u4e0b\u4e00\u6b21\u641c\u7d22\n\t\t\t\/\/strcat(dirNew, findData.name);\/\/ \u62fc\u63a5\uff1a\u5f53\u524d\u6587\u4ef6\u5939\u76ee\u5f55\\\u6587\u4ef6\u540d\n\t\t\t\/\/listFiles(dirNew);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tchar oldname&#91;100], newname&#91;100];\n\t\t\tstrcpy(oldname, dir);\n\t\t\tstrcat(oldname, \"\\\\\");\n\t\t\tstrcpy(newname, oldname);\n\t\t\tstrcat(oldname, findData.name);\n\t\t\tstrcat(newname, \"newname\");\/\/newname\u6539\u4e3a\u6587\u4ef6\u7684\u65b0\u540d\u5b57\n\t\t\tif (rename(oldname, newname) == 0)\n\t\t\t\tcout &lt;&lt; \"\u4fee\u6539\u6210\u529f\\n\";\n\t\t\telse\n\t\t\t\tperror(\"rename\");\n\t\t}\n\t} while (_findnext(handle, &amp;findData) == 0);\n\t_findclose(handle);    \/\/ \u5173\u95ed\u641c\u7d22\u53e5\u67c4\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">findfirst \u548c _findnext \u67e5\u627e\u6587\u4ef6\u539f\u7406<\/h2>\n\n\n\n<p>\u4e00\u3001\u8fd9\u4e24\u4e2a\u51fd\u6570\u5747\u5728io.h\u91cc\u9762\u3002<\/p>\n\n\n\n<p>\u4e8c\u3001\u9996\u5148\u4e86\u89e3\u4e00\u4e0b\u6587\u4ef6\u7ed3\u6784\u4f53\uff1a<br>struct _finddata_t {<br>    unsigned attrib;<br>    time_t time_create;<br>    time_t time_access;<br>    time_t time_write;<br>    _fsize_t size;<br>    char name[260];<br>};<\/p>\n\n\n\n<p>attrib\uff0c\u5c31\u662f\u6240\u67e5\u627e\u6587\u4ef6\u7684\u5c5e\u6027\u5305\u62ec\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_A_NORMAL 0x00 \/\/ Normal file - No read\/write restrictions(\u666e\u901a\u6587\u4ef6,\u6ca1\u6709\u53ea\u8bfb\/\u5199\u6743\u9650)\n_A_RDONLY 0x01 \/\/ Read only file(\u53ea\u8bfb\u6587\u4ef6)\n_A_HIDDEN 0x02 \/\/ Hidden file(\u9690\u85cf\u6587\u4ef6)\n_A_SYSTEM 0x04 \/\/ System file(\u7cfb\u7edf\u6587\u4ef6)\n_A_SUBDIR 0x10 \/\/ Subdirectory(\u5b50\u76ee\u5f55)\n_A_ARCH   0x20 \/\/ Archive file(\u5b58\u6863\u6587\u4ef6)<\/code><\/pre>\n\n\n\n<p>time_create\u3001time_access\u548ctime_write\u5206\u522b\u662f\u521b\u5efa\u6587\u4ef6\u7684\u65f6\u95f4\u3001\u6700\u540e\u4e00\u6b21\u8bbf\u95ee\u6587\u4ef6\u7684\u65f6\u95f4\u548c\u6587\u4ef6\u6700\u540e\u88ab\u4fee\u6539\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>size\uff1a\u6587\u4ef6\u5927\u5c0f<\/p>\n\n\n\n<p>name\uff1a\u6587\u4ef6\u540d<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">_findfirst\u51fd\u6570<\/h3>\n\n\n\n<p>_findfirst\u51fd\u6570\uff1along _findfirst(const char *, struct _finddata_t *);<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6587\u4ef6\u540d(\u5e26\u6587\u4ef6\u5730\u5740)\uff0c\u53ef\u4ee5\u7528&#8221;<em>.<\/em>&#8220;\u6765\u67e5\u627e\u6240\u6709\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u7528&#8221;*.cpp&#8221;\u6765\u67e5\u627e.cpp\u6587\u4ef6\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f_finddata_t\u7ed3\u6784\u4f53\u6307\u9488\u3002\u82e5\u67e5\u627e\u6210\u529f\uff0c\u8fd4\u56de\u6587\u4ef6\u53e5\u67c4\uff0c\u82e5\u5931\u8d25\uff0c\u8fd4\u56de-1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">_findnext\u51fd\u6570<\/h3>\n\n\n\n<p>_findnext\u51fd\u6570\uff1aint _findnext(handle, struct _finddata_t *);<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u6587\u4ef6\u53e5\u67c4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u540c\u6837\u4e3a_finddata_t\u7ed3\u6784\u4f53\u6307\u9488\u3002\u82e5\u67e5\u627e\u6210\u529f\uff0c\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de-1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">_findclose()\u51fd\u6570<\/h3>\n\n\n\n<p>_findclose()\u51fd\u6570\uff1aint _findclose(handle);<\/p>\n\n\n\n<p>\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u6587\u4ef6\u53e5\u67c4\u3002\u82e5\u5173\u95ed\u6210\u529f\u8fd4\u56de0\uff0c\u5931\u8d25\u8fd4\u56de-1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">rename()\u51fd\u6570<\/h2>\n\n\n\n<p>rename()\u51fd\u6570\uff0c\u529f\u80fd\u662f\u7ed9\u4e00\u4e2a\u6587\u4ef6\u91cd\u547d\u540d<\/p>\n\n\n\n<p>rename() \u51fd\u6570\u7684\u58f0\u660e:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int rename(const char *old_filename, const char *new_filename)\n<\/code><\/pre>\n\n\n\n<p>old_filename \/\/ \u8fd9\u662f C \u5b57\u7b26\u4e32\uff0c\u5305\u542b\u4e86\u8981\u88ab\u91cd\u547d\u540d\/\u79fb\u52a8\u7684\u6587\u4ef6\u540d\u79f0\u3002<br>new_filename \/\/ \u8fd9\u662f C \u5b57\u7b26\u4e32\uff0c\u5305\u542b\u4e86\u6587\u4ef6\u7684\u65b0\u540d\u79f0\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u4fee\u6539\u7684\u6587\u4ef6\u4e0d\u5728\u9879\u76ee\u7684\u76ee\u5f55\u4e0b\uff0c\u5219<\/p>\n\n\n\n<p>old_filename \u4e0e new_filename \u8981\u5e26\u4e0a\u5b8c\u6574\u7684\u8def\u5f84\u4fe1\u606f\uff0c\u5982 C:\\data\\test.txt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rename(\"C:\\\\data\\\\test.txt\",\"C:\\\\test1.txt\")<\/code><\/pre>\n\n\n\n<p>\u5c06C:\\data\\\u4e0b\u7684test.txt\u79fb\u52a8\u5230C:\\\u4e0b\uff0c\u5e76\u4e14\u5c06\u6587\u4ef6\u540d\u6539\u4e3atest1.txt<\/p>\n","protected":false},"excerpt":{"rendered":"<p>findfirst \u548c _findnext \u67e5\u627e\u6587\u4ef6\u539f\u7406 \u4e00\u3001\u8fd9\u4e24\u4e2a\u51fd\u6570\u5747\u5728io.h\u91cc\u9762\u3002 \u4e8c\u3001\u9996\u5148\u4e86\u89e3\u4e00\u4e0b\u6587 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13,14],"class_list":["post-454","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","tag-c-c","tag-14"],"_links":{"self":[{"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/posts\/454","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lolife.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=454"}],"version-history":[{"count":2,"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/posts\/454\/revisions\/456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lolife.top\/index.php?rest_route=\/wp\/v2\/media\/351"}],"wp:attachment":[{"href":"https:\/\/lolife.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lolife.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lolife.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}