{"id":56611,"date":"2025-08-14T20:45:30","date_gmt":"2025-08-14T12:45:30","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/56611.html"},"modified":"2025-08-14T20:45:30","modified_gmt":"2025-08-14T12:45:30","slug":"linux%e8%bd%af%e4%bb%b6%e7%bc%96%e7%a8%8b3-%ef%bc%88%e6%96%87%e4%bb%b6io%e5%92%8c%e7%9b%ae%e5%bd%95io%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/56611.html","title":{"rendered":"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09"},"content":{"rendered":"<h2>1.\u6587\u4ef6IO\u7684\u6982\u5ff5<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6807\u51c6IO\u6709\u7f13\u5b58&#xff0c;\u6587\u4ef6IO\u65e0\u7f13\u5b58&#xff0c;\u591a\u7528\u4e8e\u786c\u4ef6\u64cd\u4f5c\u548c\u901a\u4fe1\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6807\u51c6IO\u662f\u5e93\u51fd\u6570&#xff0c;\u6587\u4ef6IO\u662f\u7cfb\u7edf\u6548\u7528\u3002<\/p>\n<h2>2.\u7cfb\u7edf\u8c03\u7528\u4e0e\u5e93\u51fd\u6570<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7cfb\u7edf\u8c03\u7528:\u662fLinux\u5185\u6838\u4e2d\u7684\u4ee3\u7801&#xff0c;\u53ea\u80fd\u5728Linux\u7cfb\u7edf\u4e2d\u4f7f\u7528<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5e93\u51fd\u6570:\u662f\u5bf9\u7cfb\u7edf\u8c03\u7528\u7684\u5c01\u88c5&#xff0c;\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u5b89\u88c5\u5e76\u4f7f\u7528&#xff0c;\u5e93\u51fd\u6570\u6700\u7ec8\u8fd8\u662f\u8981\u8c03\u7528\u7cfb\u7edf\u8c03\u7528\u5b8c\u6210\u5bf9\u5e94\u529f\u80fd<\/p>\n<h2>3.\u6587\u4ef6IO\u51fd\u6570\u63a5\u53e3<\/h2>\n<h3>3.1open\u6253\u5f00\u6587\u4ef6<\/h3>\n<p>\u539f\u578b&#xff1a;<br \/>\n    int open(const char *pathname, int flags);<br \/>\n    int open(const char *pathname, int flags, mode_t mode);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u6253\u5f00\u6587\u4ef6\u83b7\u5f97\u64cd\u4f5c\u6587\u4ef6\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    pathname:\u8981\u6253\u5f00\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n    flags:\u6253\u5f00\u6587\u4ef6\u7684\u6807\u5fd7&#xff0c;\u5fc5\u987b\u5305\u542b\u4e09\u8005\u4e4b\u4e00<br \/>\n        O_RDONLY<br \/>\n        O_WRONLY<br \/>\n        O_RDWR<br \/>\n        O_CREAT         \u6587\u4ef6\u4e0d\u5b58\u5728\u521b\u5efa&#xff08;\u6ce8\u610f\u8981\u7ed9\u5b9a\u521b\u5efa\u6587\u4ef6\u7684\u6743\u9650&#xff09;<br \/>\n        O_TRUNC         \u6587\u4ef6\u5b58\u5728\u622a\u65ad\u4e3a0&#xff08;\u6e050&#xff09;<br \/>\n        O_APPEND        \u8ffd\u52a0<br \/>\n        O_EXCL          \u6587\u4ef6\u5b58\u5728\u62a5\u9519<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u65b0\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    \u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e09\u4e2a\u7279\u6b8a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\u6807\u51c6\u8f93\u5165&#xff08;0&#xff09;\u3001\u6807\u51c6\u8f93\u51fa&#xff08;1&#xff09;\u3001\u6807\u51c6\u9519\u8bef&#xff08;2&#xff09;\u3002<\/p>\n<h3>3.2close\u5173\u95ed\u6587\u4ef6<\/h3>\n<p>\u6ce8\u610f&#xff1a;<br \/>\n\u6709\u4e09\u4e2a\u7279\u6b8a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff1a;\u6807\u51c6\u8f93\u5165&#xff08;0&#xff09;\u3001\u6807\u51c6\u8f93\u51fa&#xff08;1&#xff09;\u3001\u6807\u51c6\u9519\u8bef&#xff08;2&#xff09;<br \/>\n\u6587\u4ef6\u63cf\u8ff0\u7b26\u7279\u70b9&#xff1a;<br \/>\n\u975e\u8d1f\u6574\u6570<\/p>\n<p>\u6253\u5f00\u5e76\u5173\u95ed\u6587\u4ef6&#xff1a;<\/p>\n<p>#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    int fd &#061; 0;<\/p>\n<p>    \/\/0664<br \/>\n    \/\/110110100<br \/>\n    \/\/rw-rw-r&#8211;<br \/>\n    fd &#061; open(&#034;a.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0664);<br \/>\n    if (-1 &#061;&#061; fd)<br \/>\n    {<br \/>\n        perror(&#034;fail to open&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    printf(&#034;fd &#061; %d\\\\n&#034;, fd);<\/p>\n<p>    close(fd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>3.3\u6807\u51c6IO\u5bf9\u5e94\u7684\u6587\u4ef6IO\u7684\u6253\u5f00\u65b9\u5f0f<\/h3>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<tbody>\n<tr>\n<td>\u6807\u51c6IO<\/td>\n<td>\u6587\u4ef6IO<\/td>\n<\/tr>\n<tr>\n<td>r<\/td>\n<td>O_RDONLY<\/td>\n<\/tr>\n<tr>\n<td>r&#043;<\/td>\n<td>O_RDWR<\/td>\n<\/tr>\n<tr>\n<td>w<\/td>\n<td>O_WRONLY | O_CREAT | O_TRUNC , 0664<\/td>\n<\/tr>\n<tr>\n<td>w&#043;<\/td>\n<td>O_RDWR | O_CREAT | O_TRUNC, 0664<\/td>\n<\/tr>\n<tr>\n<td>a<\/td>\n<td>O_WRONLY | O_APPEND | O_CREAT, 0664<\/td>\n<\/tr>\n<tr>\n<td>a&#043;<\/td>\n<td>O_RDWR | O_APPEND | O_CREAT, 0664<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3.4write\u5199\u5165<\/h3>\n<p>\u539f\u578b&#xff1a;ssize_t write(int fd, const void *buf, size_t count);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u5411\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u5199\u5165buf\u6307\u5411\u7684count\u4e2a\u5b57\u8282\u7684\u6570\u636e<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    fd:\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    buf:\u8981\u5199\u5165\u7684\u6570\u636e\u7a7a\u95f4\u9996\u5730\u5740<br \/>\n    count:\u8981\u5199\u5165\u7684\u5b57\u8282\u6570<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u5b9e\u9645\u5199\u5165\u7684\u5b57\u8282\u6570<br \/>\n    \u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    int fd &#061; 0;<br \/>\n    char tmpbuff[4096] &#061; {&#034;hello world&#034;};<br \/>\n    ssize_t nret &#061; 0;<\/p>\n<p>    fd &#061; open(&#034;a.txt&#034;, O_WRONLY | O_CREAT | O_TRUNC, 0664);<br \/>\n    if (-1 &#061;&#061; fd)<br \/>\n    {<br \/>\n        perror(&#034;fail to open&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    nret &#061; write(fd, tmpbuff, strlen(tmpbuff));<br \/>\n    printf(&#034;\u5b9e\u9645\u5199\u5165:%ld\\\\n&#034;, nret);<\/p>\n<p>    close(fd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>3.5read\u8bfb\u53d6<\/h3>\n<p>\u539f\u578b&#xff1a;ssize_t read(int fd, void *buf, size_t count);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u8bfb\u53d6count\u4e2a\u5b57\u8282\u653e\u5165buf\u6307\u5411\u7684\u7a7a\u95f4\u4e2d<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    fd:\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    buf:\u5b58\u653e\u6570\u636e\u7a7a\u95f4\u7684\u9996\u5730\u5740<br \/>\n    count:\u60f3\u8981\u8bfb\u53d6\u7684\u5b57\u8282\u6570<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u5b9e\u9645\u8bfb\u5230\u7684\u5b57\u8282\u6570<br \/>\n    \u8bfb\u5230\u6587\u4ef6\u672b\u5c3e\u8fd4\u56de0<br \/>\n    \u8bfb\u53d6\u51fa\u9519\u8fd4\u56de-1<\/p>\n<p>\u8bfb\u53d6\/usr\/include\/stdio.h\u8def\u5f84\u4e0b4096\u4e2a\u5b57\u8282&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    int fd &#061; 0;<br \/>\n    char tmpbuff[4096] &#061; {0};<br \/>\n    ssize_t nret &#061; 0;<\/p>\n<p>    fd &#061; open(&#034;\/usr\/include\/stdio.h&#034;, O_RDONLY);<br \/>\n    if (-1 &#061;&#061; fd)<br \/>\n    {<br \/>\n        perror(&#034;fail to open&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    nret &#061; read(fd, tmpbuff, sizeof(tmpbuff));<br \/>\n    printf(&#034;\u5b9e\u9645\u63a5\u6536 %ld \u5b57\u8282, \u5185\u5bb9:%s\\\\n&#034;, nret, tmpbuff);<\/p>\n<p>    close(fd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u7528read\u548cwrite\u8fdb\u884c\u6587\u4ef6\u7684\u62f7\u8d1d&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    FILE *fp1 &#061; NULL;<br \/>\n    FILE *fp2 &#061; NULL;<br \/>\n    char src[256] &#061; {0};<br \/>\n    char dst[256] &#061; {0};<br \/>\n    char tmpbuff[4060] &#061;{0};<br \/>\n    printf(&#034;\u8bf7\u8f93\u5165\u8981\u62f7\u8d1d\u7684\u6587\u4ef6\u540d:&#034;);<br \/>\n    m_fgets(src);<br \/>\n    printf(&#034;\u8bf7\u8f93\u5165\u76ee\u7684\u6587\u4ef6\u540d:&#034;);<br \/>\n    m_fgets(dst);<br \/>\n    char a[4096]&#061;{0};<br \/>\n    ssize_t nret;<br \/>\n    fp1 &#061; open(src, O_RDONLY);<br \/>\n    if (NULL &#061;&#061; fp1)<br \/>\n    {<br \/>\n        perror(&#034;fail to open1&#034;);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    fp2 &#061; open (dst,O_WRONLY | O_CREAT | O_TRUNC ,0664);<br \/>\n    if (NULL &#061;&#061; fp2)<br \/>\n    {<br \/>\n        perror(&#034;fail to open2&#034;);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    while (nret &#061; read(fp1,tmpbuff,sizeof(tmpbuff)))<br \/>\n    {<br \/>\n        write(fp2,tmpbuff,nret);<br \/>\n    }<\/p>\n<p>    close(fp1);<br \/>\n    close(fp2);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3>3.6lseek\u6587\u4ef6\u63cf\u8ff0\u7b26\u504f\u79fb\u91cf\u7684\u5b9a\u4f4d<\/h3>\n<p>\u539f\u578b&#xff1a;off_t lseek(int fd, off_t offset, int whence);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u91cd\u65b0\u5b9a\u4f4d\u6d41\u7684\u504f\u79fb\u91cf<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    fd:\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\n    offset:\u504f\u79fb\u91cf<br \/>\n    whence:<br \/>\n        SEEK_SET<br \/>\n        SEEK_CUR<br \/>\n        SEEK_END<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u504f\u79fb\u91cf<br \/>\n    \u5931\u8d25\u8fd4\u56de-1<\/p>\n<p>\u504f\u79fb\u6253\u5370\u5e76\u83b7\u53d6\u5f53\u524d\u504f\u79fb\u91cf<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    int fd &#061; 0;<br \/>\n    char ch &#061; 0;<br \/>\n    off_t len &#061; 0;<\/p>\n<p>    fd &#061; open(&#034;a.txt&#034;, O_WRONLY | O_CREAT, 0664);<br \/>\n    if (-1 &#061;&#061; fd)<br \/>\n    {<br \/>\n        perror(&#034;fail to open&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    len &#061; lseek(fd, 10, SEEK_SET);<br \/>\n    printf(&#034;\u504f\u79fb\u91cf:%ld\\\\n&#034;, len);<br \/>\n    ch &#061; &#039;a&#039;;<br \/>\n    write(fd, &amp;ch, 1);<br \/>\n    len &#061; lseek(fd, 0, SEEK_CUR);<br \/>\n    printf(&#034;\u504f\u79fb\u91cf:%ld\\\\n&#034;, len);<\/p>\n<p>    len &#061; lseek(fd, -5, SEEK_CUR);<br \/>\n    printf(&#034;\u504f\u79fb\u91cf:%ld\\\\n&#034;, len);<br \/>\n    ch &#061; &#039;b&#039;;<br \/>\n    write(fd, &amp;ch, 1);<\/p>\n<p>    len &#061; lseek(fd, 0, SEEK_SET);<br \/>\n    printf(&#034;\u504f\u79fb\u91cf:%ld\\\\n&#034;, len);<br \/>\n    ch &#061; &#039;c&#039;;<br \/>\n    write(fd, &amp;ch, 1);  <\/p>\n<p>    close(fd);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"85\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda687ce3a.png\" width=\"289\" \/>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"83\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda6897a00.png\" width=\"171\" \/><\/p>\n<h2>3.7\u6587\u4ef6IO\u4e0e\u6807\u51c6IO\u4e92\u76f8\u8f6c\u6362\u7684\u51fd\u6570<\/h2>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:500px\">\n<tbody>\n<tr>\n<td>\u51fd\u6570\u63a5\u53e3<\/td>\n<td>\u529f\u80fd<\/td>\n<\/tr>\n<tr>\n<td>fileno<\/td>\n<td>\u6839\u636e\u6587\u4ef6\u6d41\u6307\u9488\u83b7\u5f97\u6587\u4ef6\u63cf\u8ff0\u7b26<\/td>\n<\/tr>\n<tr>\n<td>fdopen<\/td>\n<td>\u6839\u636e\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u83b7\u5f97\u6587\u4ef6\u6d41\u6307\u9488<\/td>\n<\/tr>\n<tr>\n<td>feof<\/td>\n<td>\u5224\u65ad\u6587\u4ef6\u6d41\u6307\u9488\u662f\u5426\u5230\u8fbe\u672b\u5c3e<\/td>\n<\/tr>\n<tr>\n<td>ferror<\/td>\n<td>\u5224\u65ad\u6587\u4ef6\u6d41\u6307\u9488\u662f\u5426\u51fa\u9519<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u5c0f\u9879\u76ee&#xff1a;\u5355\u8bcd\u67e5\u627e<\/h2>\n<p>\u9700\u8981\u4ecedict.txt\u6587\u672c\u6587\u4ef6\u4e2d\u67e5\u627e\u4ece\u7ec8\u7aef\u8f93\u5165\u7684\u5355\u8bcd&#xff0c;\u5e76\u8f93\u51fa\u5176\u610f\u601d\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"755\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda68aa7a4.png\" width=\"981\" \/><\/p>\n<p>#include&#034;head.h&#034;<br \/>\nint main(void)<br \/>\n{<br \/>\n    char in_put_word[256] &#061; {0};<br \/>\n    char tmpbuff[1024] &#061; {0};<br \/>\n    char word[256] &#061; {0};<br \/>\n    char explanation[256] &#061; {0};<br \/>\n    int a &#061; 0;<br \/>\n    printf(&#034;\u8bf7\u8f93\u5165\u4f60\u8981\u67e5\u8be2\u7684\u5355\u8bcd:\\\\n&#034;);<br \/>\n    gets(in_put_word);<br \/>\n    in_put_word[strlen(in_put_word)]&#061; &#039;\\\\0&#039;;<br \/>\n    FILE *fp &#061; NULL;<br \/>\n    fp &#061; fopen(&#034;dict.txt&#034;, &#034;r&#034;);<br \/>\n    if (NULL &#061;&#061; fp)<br \/>\n    {<br \/>\n        perror(&#034;fail to fopen&#034;);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    while(1)<br \/>\n    {<br \/>\n        if(fgets(tmpbuff, sizeof(tmpbuff), fp) &#061;&#061; NULL)<br \/>\n        {<br \/>\n            printf(&#034;no found\\\\n&#034;);<br \/>\n            break;<br \/>\n        }<br \/>\n        \/\/\u5c06fgets\u8bfb\u5230\u7684\u5185\u5bb9\u5206\u5272&#xff0c;\u524d\u534a\u90e8\u5206\u4e3a255\u4e2a\u5b57\u8282\u7684\u5185\u5bb9&#xff0c;\u9047\u5230\u7a7a\u683c\u622a\u6b62\u5b58\u5728word\u4e2d<br \/>\n        \/\/\u540e\u534a\u90e8\u5206\u8bfb\u53d6255\u4e2a\u5b57\u8282\u5185\u5bb9&#xff0c;\u9047\u5230\\\\n\u622a\u6b62,\u5b58\u5728explanation\u4e2d<br \/>\n        sscanf(tmpbuff, &#034;%255s %255[^\\\\n]&#034;, word, explanation);<br \/>\n        if(strcmp(in_put_word, word) &#061;&#061; 0)<br \/>\n        {<br \/>\n            printf(&#034;\u5355\u8bcd\u542b\u4e49:%s\\\\n&#034;,explanation);<br \/>\n            a &#061; 1;<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n    fclose(fp);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"231\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda68e4247.png\" width=\"754\" \/><\/p>\n<\/p>\n<h2>4.\u76ee\u5f55IO<\/h2>\n<h3>4.1\u64cd\u4f5c\u65b9\u5f0f<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01&#xff09;\u6253\u5f00\u76ee\u5f55\u6587\u4ef6 2&#xff09;\u8bfb\u53d6\u76ee\u5f55\u6587\u4ef6\u4e2d\u7684\u76ee\u5f55\u9879 3&#xff09;\u5173\u95ed\u76ee\u5f55\u6587\u4ef6<\/p>\n<h3>4.2\u51fd\u6570\u63a5\u53e3<\/h3>\n<h4>4.2.1opendir\u6253\u5f00\u76ee\u5f55\u6d41\u83b7\u5f97\u76ee\u5f55\u6d41\u6307\u9488<\/h4>\n<p>\u539f\u578b&#xff1a;DIR *opendir(const char *name);<br \/>\n \u529f\u80fd&#xff1a;<br \/>\n    \u6253\u5f00\u76ee\u5f55\u6d41\u83b7\u5f97\u76ee\u5f55\u6d41\u6307\u9488<br \/>\n \u53c2\u6570&#xff1a;<br \/>\n    name:\u76ee\u5f55\u6587\u4ef6\u7684\u8def\u5f84<br \/>\n \u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u76ee\u5f55\u6d41\u6307\u9488<br \/>\n    \u5931\u8d25\u8fd4\u56deNULL<\/p>\n<h4>4.2.2closedir\u5173\u95ed\u76ee\u5f55\u6d41\u6307\u9488<\/h4>\n<p>\u539f\u578b&#xff1a;int closedir(DIR *dirp);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u5173\u95ed\u76ee\u5f55\u6d41\u6307\u9488<\/p>\n<h4>4.2.3readdir\u8bfb\u53d6\u5e76\u8fd4\u56de\u4e0b\u4e00\u4e2a\u76ee\u5f55\u9879\u7684\u4fe1\u606f<\/h4>\n<p>\u539f\u578b&#xff1a;struct dirent *readdir(DIR *dirp);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u8bfb\u53d6\u5e76\u8fd4\u56de\u4e0b\u4e00\u4e2a\u76ee\u5f55\u9879\u7684\u4fe1\u606f<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    dirp:\u76ee\u5f55\u6d41\u6307\u9488<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u5305\u542b\u76ee\u5f55\u9879\u4fe1\u606f\u7ed3\u6784\u4f53\u6307\u9488<br \/>\n    \u5931\u8d25\u8fd4\u56deNULL<br \/>\n    \u8bfb\u5230\u6587\u4ef6\u672b\u5c3e\u8fd4\u56deNULL<\/p>\n<p>struct dirent {<br \/>\n   ino_t          d_ino;       \/* Inode number:\u901a\u8fc7inode\u627e\u5230\u6587\u4ef6\u5bf9\u5e94\u7684\u78c1\u76d8\u7a7a<br \/>\n\u95f4\u4f4d\u7f6e *\/<br \/>\n   off_t          d_off;       \/* Not an offset; see below *\/<br \/>\n   unsigned short d_reclen;    \/* Length of this record *\/<br \/>\n   unsigned char  d_type;      \/* Type of file; not supported<br \/>\n                                  by all filesystem types&#xff1a;\u6587\u4ef6\u7c7b\u578b *\/<br \/>\n   char           d_name[256]; \/* Null-terminated filename&#xff1a; \u6587\u4ef6\u540d*\/<br \/>\n };<\/p>\n<p>\u8fd4\u56de\u8f93\u5165\u8def\u5f84\u6587\u4ef6\u7684\u5c5e\u6027&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    DIR *dp &#061; NULL;<br \/>\n    struct dirent *pp &#061; NULL;<br \/>\n    char filepath[256] &#061; {0};<\/p>\n<p>    printf(&#034;\u8bf7\u8f93\u5165\u8def\u5f84:\\\\n&#034;);<br \/>\n    m_fgets(filepath);<\/p>\n<p>    dp &#061; opendir(filepath);<br \/>\n    if (NULL &#061;&#061; dp)<br \/>\n    {<br \/>\n        perror(&#034;fail to opendir&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    while (1)<br \/>\n    {<br \/>\n        pp &#061; readdir(dp);<br \/>\n        if (NULL &#061;&#061; pp)<br \/>\n        {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        if (&#039;.&#039; &#061;&#061; pp-&gt;d_name[0])<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        switch (pp-&gt;d_type)<br \/>\n        {<br \/>\n            case DT_BLK:printf(&#034;\u5757\u8bbe\u5907\u6587\u4ef6&#034;);break;<br \/>\n            case DT_CHR:printf(&#034;\u5b57\u7b26\u8bbe\u5907\u6587\u4ef6&#034;);break;<br \/>\n            case DT_DIR:printf(&#034;\u76ee\u5f55\u6587\u4ef6&#034;);break;<br \/>\n            case DT_FIFO:printf(&#034;\u7ba1\u9053\u6587\u4ef6&#034;);break;<br \/>\n            case DT_LNK:printf(&#034;\u94fe\u63a5\u6587\u4ef6&#034;);break;<br \/>\n            case DT_REG:printf(&#034;\u666e\u901a\u6587\u4ef6&#034;);break;<br \/>\n            case DT_SOCK:printf(&#034;\u5957\u63a5\u5b57\u6587\u4ef6&#034;);break;<br \/>\n        }<\/p>\n<p>        printf(&#034;\\\\t\\\\t%s\\\\t\\\\t%s\/%s\\\\n&#034;, pp-&gt;d_name, filepath, pp-&gt;d_name);<br \/>\n    }<\/p>\n<p>    closedir(dp);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>4.2.4getcwd\u83b7\u5f97\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84<\/h4>\n<p>\u539f\u578b&#xff1a;char *getcwd(char *buf, size_t size);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u83b7\u5f97\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    buf:\u5b58\u653e\u8def\u5f84\u5b57\u7b26\u4e32\u7a7a\u95f4\u9996\u5730\u5740<br \/>\n    size:\u6700\u591a\u5b58\u653e\u5b57\u7b26\u4e32\u7684\u5927\u5c0f<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u5305\u542b\u5b57\u7b26\u4e32\u7a7a\u95f4\u7684\u9996\u5730\u5740<br \/>\n    \u5931\u8d25\u8fd4\u56deNULL<\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    char tmpbuff[4096] &#061; {0};<br \/>\n    FILE *fp &#061; NULL;<\/p>\n<p>    getcwd(tmpbuff, sizeof(tmpbuff));<br \/>\n    printf(&#034;\u5f53\u524d\u8def\u5f84:%s\\\\n&#034;, tmpbuff);<\/p>\n<p>    mkdir(&#034;tmp&#034;, 0777);<br \/>\n    rmdir(&#034;tmp&#034;);<\/p>\n<p>    chdir(&#034;..&#034;);<\/p>\n<p>    getcwd(tmpbuff, sizeof(tmpbuff));<br \/>\n    printf(&#034;\u5f53\u524d\u8def\u5f84:%s\\\\n&#034;, tmpbuff);<\/p>\n<p>    fclose(fopen(&#034;file.txt&#034;, &#034;w&#034;));<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"154\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124529-689dda6905566.png\" width=\"511\" \/><\/p>\n<\/p>\n<p>\u6df1\u5ea6\u6709\u9650\u904d\u5386\u6240\u6709\u6587\u4ef6\u5e76\u6253\u5370\u8def\u5f84&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>void listdir(char *dirpath)<br \/>\n{<br \/>\n    DIR *dp &#061; NULL;<br \/>\n    struct dirent *pp &#061; NULL;<br \/>\n    char tmpbuff[4096] &#061; {0};<\/p>\n<p>    dp &#061; opendir(dirpath);<br \/>\n    if (NULL &#061;&#061; dp)<br \/>\n    {<br \/>\n        perror(&#034;fail to opendir&#034;);<br \/>\n        printf(&#034;failed:%s\\\\n&#034;, dirpath);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    while (1)<br \/>\n    {<br \/>\n        pp &#061; readdir(dp);<br \/>\n        if (NULL &#061;&#061; pp)<br \/>\n        {<br \/>\n            break;<br \/>\n        }<\/p>\n<p>        if (&#039;.&#039; &#061;&#061; pp-&gt;d_name[0])<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        sprintf(tmpbuff, &#034;%s\/%s&#034;, dirpath, pp-&gt;d_name);<br \/>\n        printf(&#034;%s\\\\n&#034;, tmpbuff);<\/p>\n<p>        if (pp-&gt;d_type &#061;&#061; DT_DIR)<br \/>\n        {<br \/>\n            listdir(tmpbuff);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    closedir(dp);<\/p>\n<p>    return;<br \/>\n}<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    listdir(&#034;\/home\/linux&#034;);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u6240\u6709\u6587\u4ef6\u5e76\u6253\u5370\u8def\u5f84&#xff1a;<\/p>\n<p>#include &#034;head.h&#034;           \/\/ \u5305\u542b\u81ea\u5b9a\u4e49\u5934\u6587\u4ef6 head.h&#xff08;\u53ef\u80fd\u5305\u542b\u901a\u7528\u5b9a\u4e49\u6216\u51fd\u6570\u58f0\u660e&#xff09;<br \/>\n#include &#034;linkqueue.h&#034;       \/\/ \u5305\u542b\u94fe\u5f0f\u961f\u5217\u7684\u5b9e\u73b0\u5934\u6587\u4ef6<\/p>\n<p>void listdir(char *dirpath)  \/\/ \u5b9a\u4e49\u904d\u5386\u76ee\u5f55\u7684\u51fd\u6570&#xff0c;\u53c2\u6570\u4e3a\u76ee\u5f55\u8def\u5f84<br \/>\n{<br \/>\n    linknode *plinkqueue &#061; NULL;  \/\/ \u521d\u59cb\u5316\u961f\u5217\u6307\u9488<br \/>\n    char tmppath[256] &#061; {0};      \/\/ \u4e34\u65f6\u5b58\u50a8\u51fa\u961f\u7684\u76ee\u5f55\u8def\u5f84&#xff08;\u7f13\u51b2\u533a\u5927\u5c0f256\u5b57\u8282&#xff09;<br \/>\n    char filepath[1024] &#061; {0};    \/\/ \u5b58\u50a8\u5b8c\u6574\u6587\u4ef6\u8def\u5f84&#xff08;\u7f13\u51b2\u533a\u5927\u5c0f1024\u5b57\u8282&#xff09;<br \/>\n    DIR *dp &#061; NULL;               \/\/ \u76ee\u5f55\u6d41\u6307\u9488<br \/>\n    struct dirent *pp &#061; NULL;     \/\/ \u76ee\u5f55\u6761\u76ee\u6307\u9488<\/p>\n<p>    plinkqueue &#061; create_empty_linkqueue();  \/\/ \u521b\u5efa\u7a7a\u961f\u5217<br \/>\n    enter_linkqueue(plinkqueue, dirpath);   \/\/ \u5c06\u521d\u59cb\u76ee\u5f55\u8def\u5f84\u5165\u961f<\/p>\n<p>    while (!is_empty_linkqueue(plinkqueue))  \/\/ \u5faa\u73af\u76f4\u5230\u961f\u5217\u4e3a\u7a7a<br \/>\n    {<br \/>\n        quit_linkqueue(plinkqueue, tmppath);  \/\/ \u4ece\u961f\u5934\u53d6\u51fa\u4e00\u4e2a\u76ee\u5f55\u8def\u5f84\u5230 tmppath<\/p>\n<p>        dp &#061; opendir(tmppath);  \/\/ \u5c1d\u8bd5\u6253\u5f00\u76ee\u5f55<br \/>\n        if (NULL &#061;&#061; dp)          \/\/ \u6253\u5f00\u5931\u8d25\u5904\u7406<br \/>\n        {<br \/>\n            perror(&#034;fail to opendir&#034;);  \/\/ \u6253\u5370\u9519\u8bef\u4fe1\u606f<br \/>\n            printf(&#034;%s\\\\n&#034;, tmppath);    \/\/ \u8f93\u51fa\u5931\u8d25\u8def\u5f84<br \/>\n            return;                     \/\/ \u76f4\u63a5\u8fd4\u56de&#xff08;\u53ef\u80fd\u9700\u6539\u4e3a continue \u4ee5\u7ee7\u7eed\u5904\u7406\u5176\u4ed6\u76ee\u5f55&#xff09;<br \/>\n        }<\/p>\n<p>        while (1)  \/\/ \u5faa\u73af\u8bfb\u53d6\u76ee\u5f55\u6761\u76ee<br \/>\n        {<br \/>\n            pp &#061; readdir(dp);  \/\/ \u8bfb\u53d6\u4e0b\u4e00\u4e2a\u76ee\u5f55\u6761\u76ee<br \/>\n            if (NULL &#061;&#061; pp)   \/\/ \u5982\u679c\u6761\u76ee\u4e3a\u7a7a&#xff08;\u904d\u5386\u5b8c\u6bd5&#xff09;<br \/>\n            {<br \/>\n                break;       \/\/ \u9000\u51fa\u5faa\u73af<br \/>\n            }<\/p>\n<p>            if (&#039;.&#039; &#061;&#061; pp-&gt;d_name[0])  \/\/ \u8df3\u8fc7\u9690\u85cf\u6587\u4ef6&#xff08;\u4ee5 . \u5f00\u5934&#xff09;<br \/>\n            {<br \/>\n                continue;<br \/>\n            }<\/p>\n<p>            sprintf(filepath, &#034;%s\/%s&#034;, tmppath, pp-&gt;d_name);  \/\/ \u62fc\u63a5\u5b8c\u6574\u8def\u5f84<br \/>\n            printf(&#034;%s\\\\n&#034;, filepath);                         \/\/ \u6253\u5370\u8def\u5f84<\/p>\n<p>            if (pp-&gt;d_type &#061;&#061; DT_DIR)  \/\/ \u5982\u679c\u662f\u5b50\u76ee\u5f55<br \/>\n            {<br \/>\n                enter_linkqueue(plinkqueue, filepath);  \/\/ \u5c06\u5b50\u76ee\u5f55\u8def\u5f84\u5165\u961f<br \/>\n            }<br \/>\n        }<br \/>\n        closedir(dp);  \/\/ \u5173\u95ed\u5f53\u524d\u76ee\u5f55\u6d41&#xff08;\u539f\u4ee3\u7801\u9057\u6f0f&#xff0c;\u9700\u8865\u5145&#xff09;<br \/>\n    }<\/p>\n<p>    destroy_linkqueue(&amp;plinkqueue);  \/\/ \u9500\u6bc1\u961f\u5217\u91ca\u653e\u5185\u5b58<br \/>\n    return;                         \/\/ \u51fd\u6570\u7ed3\u675f<br \/>\n}<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    listdir(&#034;\/home\/linux&#034;);  \/\/ \u4ece \/home\/linux \u5f00\u59cb\u904d\u5386\u76ee\u5f55<br \/>\n    return 0;                 \/\/ \u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa<br \/>\n}<\/p>\n<p>\u5747\u53ef\u5b9e\u73b0\u4ee5\u4e0b\u6548\u679c&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"661\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124529-689dda691a5c9.png\" width=\"586\" \/><\/p>\n<h4>4.2.5chdir\u5207\u6362\u5f53\u524d\u7684\u5de5\u4f5c\u8def\u5f84<\/h4>\n<p>\u539f\u578b&#xff1a;int chdir(const char *path);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u5207\u6362\u5f53\u524d\u7684\u5de5\u4f5c\u8def\u5f84<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    path&#xff1a;\u8981\u5207\u6362\u7684\u8def\u5f84<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de0<br \/>\n    \u5931\u8d25\u8fd4\u56de-1<\/p>\n<h4>4.2.6mkdir\u521b\u5efa\u76ee\u5f55\u6587\u4ef6<\/h4>\n<p>\u539f\u578b&#xff1a;int mkdir(const char *pathname, mode_t mode);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u521b\u5efa\u76ee\u5f55\u6587\u4ef6<br \/>\nr&#xff1a;\u8bfb\u6743\u9650&#xff0c;\u51b3\u5b9a\u7528\u6237\u662f\u5426\u80fd\u591f\u67e5\u770b\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u540d<br \/>\nw&#xff1a;\u5199\u6743\u9650&#xff0c;\u51b3\u5b9a\u7528\u6237\u662f\u5426\u80fd\u591f\u5728\u76ee\u5f55\u4e0b\u65b0\u5efa\u6587\u4ef6<br \/>\nx&#xff1a;\u6267\u884c\u6743\u9650&#xff0c;\u51b3\u5b9a\u7528\u6237\u662f\u5426\u80fd\u591f\u8fdb\u5165\u76ee\u5f55<\/p>\n<h4>4.2.7rmdir\u5220\u9664\u76ee\u5f55\u6587\u4ef6<\/h4>\n<p>\u539f\u578b&#xff1a;int rmdir(const char *pathname);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u5220\u9664\u76ee\u5f55\u6587\u4ef6<\/p>\n<h2>5.\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u63a5\u53e3<\/h2>\n<\/p>\n<h3>5.1\u65f6\u95f4\u7c7b\u578b\u5206\u7c7b<\/h3>\n<h3>5.2\u51fd\u6570\u63a5\u53e3<\/h3>\n<h4>5.2.1time<\/h4>\n<p>\u539f\u578b&#xff1a;time_t time(time_t *tloc);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u8fd4\u56de1970-1-1\u65e5\u5230\u73b0\u5728\u7684\u79d2\u6570<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    \u5b58\u653e\u79d2\u6570\u7a7a\u95f4\u7684\u9996\u5730\u5740<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u79d2\u6570<br \/>\n    \u5931\u8d25\u8fd4\u56de-1<\/p>\n<h4>5.2.2localtime<\/h4>\n<p>\u539f\u578b&#xff1a;struct tm *localtime(const time_t *timep);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n    \u5c06\u79d2\u6570\u8f6c\u6362\u4e3a\u7ed3\u6784\u4f53\u65f6\u95f4<br \/>\n\u53c2\u6570&#xff1a;<br \/>\n    timep:1970-1-1\u5230\u73b0\u5728\u7684\u79d2\u6570<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n    \u6210\u529f\u8fd4\u56de\u5305\u542b\u65f6\u95f4\u7684\u7ed3\u6784\u4f53\u6307\u9488<br \/>\n    \u5931\u8d25\u8fd4\u56deNULL<\/p>\n<p>struct tm {<br \/>\n   int tm_sec;    \/* Seconds (0-60) *\/<br \/>\n   int tm_min;    \/* Minutes (0-59) *\/<br \/>\n   int tm_hour;   \/* Hours (0-23) *\/<br \/>\n   int tm_mday;   \/* Day of the month (1-31) *\/<br \/>\n   int tm_mon;    \/* Month (0-11) *\/<br \/>\n   int tm_year;   \/* Year &#8211; 1900 *\/<br \/>\n   int tm_wday;   \/* Day of the week (0-6, Sunday &#061; 0) *\/<br \/>\n   int tm_yday;   \/* Day in the year (0-365, 1 Jan &#061; 0) *\/<br \/>\n   int tm_isdst;  \/* Daylight saving time *\/<br \/>\n };<\/p>\n<h4>5.2.3mktime<\/h4>\n<p>\u539f\u578b&#xff1a;time_t mktime(struct tm *tm);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n\u5c06\u7ed3\u6784\u4f53\u65f6\u95f4\u8f6c\u6362\u4e3atime_t\u7c7b\u578b\u7684\u65f6\u95f4<br \/>\n\u53c2\u6570&#xff1a;<br \/>\ntm:\u7ed3\u6784\u4f53\u65f6\u95f4\u7684\u9996\u5730\u5740<br \/>\n\u8fd4\u56de\u503c&#xff1a;<br \/>\n\u6210\u529f\u8fd4\u56detime_t\u65f6\u95f4<br \/>\n\u5931\u8d25\u8fd4\u56de-1 <\/p>\n<h4>5.2.4ctime<\/h4>\n<p>\u539f\u578b&#xff1a;char *ctime(const time_t *timep);<br \/>\n\u529f\u80fd&#xff1a;<br \/>\n\u5c06time_t\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65f6\u95f4<\/p>\n<p>\u5e94\u7528&#xff1a;<\/p>\n<\/p>\n<p>#include &#034;head.h&#034;<\/p>\n<p>int main(void)<br \/>\n{<br \/>\n    time_t t;<br \/>\n    time_t ret;<br \/>\n    struct tm *ptm &#061; NULL;<br \/>\n    char *pstr &#061; NULL;<\/p>\n<p>\/\/    time(&amp;t);<br \/>\n    t &#061; time(NULL);<br \/>\n    printf(&#034;t &#061; %ld\\\\n&#034;, t);<\/p>\n<p>    ptm &#061; localtime(&amp;t);<br \/>\n    printf(&#034;%04d-%02d-%02d %02d:%02d:%02d\\\\n&#034;, ptm-&gt;tm_year&#043;1900, ptm-&gt;tm_mon&#043;1, ptm-&gt;tm_mday,<br \/>\n                                              ptm-&gt;tm_hour, ptm-&gt;tm_min, ptm-&gt;tm_sec);<\/p>\n<p>    ret &#061; mktime(ptm);<br \/>\n    printf(&#034;ret &#061; %ld\\\\n&#034;, ret);<\/p>\n<p>    pstr &#061; ctime(&amp;ret);<br \/>\n    printf(&#034;pstr &#061; %s\\\\n&#034;, pstr);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb596\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86Linux\u7cfb\u7edf\u4e2d\u6587\u4ef6IO\u548c\u76ee\u5f55IO\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u64cd\u4f5c\u51fd\u6570\u3002\u6587\u4ef6IO\u90e8\u5206\u8be6\u7ec6\u8bb2\u89e3\u4e86open\u3001close\u3001read\u3001write\u3001lseek\u7b49\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u4ee5\u53ca\u4e0e\u6807\u51c6IO\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u5305\u62ec\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6743\u9650\u6807\u5fd7\u7b49\u5173\u952e\u53c2\u6570\u3002\u76ee\u5f55IO\u90e8\u5206\u6db5\u76d6opendir\u3001readdir\u3001closedir\u7b49\u76ee\u5f55\u64cd\u4f5c\u51fd\u6570\uff0c\u4ee5\u53cagetcwd\u3001chdir\u7b49\u8def\u5f84\u7ba1\u7406\u51fd\u6570\u3002\u6587\u7ae0\u8fd8\u5c55\u793a\u4e86\u6df1\u5ea6\u4f18\u5148\u548c\u5e7f\u5ea6\u4f18\u5148\u4e24\u79cd\u76ee\u5f55\u904d\u5386\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5e76\u4ecb\u7ecd\u4e86\u65f6\u95f4\u5904\u7406\u76f8\u5173\u51fd\u6570\u5982time\u3001localtime\u7b49\u3002\u901a\u8fc7\u591a\u4e2a\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u6587\u4ef6\u64cd\u4f5c\u3001\u76ee\u5f55\u904d\u5386\u548c\u65f6\u95f4\u8f6c\u6362\u7b49\u5b9e\u9645<\/p>\n","protected":false},"author":2,"featured_media":56605,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[174,58,190,43,305,427,44],"topic":[],"class_list":["post-56611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-linux","tag-190","tag-43","tag-305","tag-427","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/56611.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb596\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86Linux\u7cfb\u7edf\u4e2d\u6587\u4ef6IO\u548c\u76ee\u5f55IO\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u64cd\u4f5c\u51fd\u6570\u3002\u6587\u4ef6IO\u90e8\u5206\u8be6\u7ec6\u8bb2\u89e3\u4e86open\u3001close\u3001read\u3001write\u3001lseek\u7b49\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u4ee5\u53ca\u4e0e\u6807\u51c6IO\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u5305\u62ec\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6743\u9650\u6807\u5fd7\u7b49\u5173\u952e\u53c2\u6570\u3002\u76ee\u5f55IO\u90e8\u5206\u6db5\u76d6opendir\u3001readdir\u3001closedir\u7b49\u76ee\u5f55\u64cd\u4f5c\u51fd\u6570\uff0c\u4ee5\u53cagetcwd\u3001chdir\u7b49\u8def\u5f84\u7ba1\u7406\u51fd\u6570\u3002\u6587\u7ae0\u8fd8\u5c55\u793a\u4e86\u6df1\u5ea6\u4f18\u5148\u548c\u5e7f\u5ea6\u4f18\u5148\u4e24\u79cd\u76ee\u5f55\u904d\u5386\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5e76\u4ecb\u7ecd\u4e86\u65f6\u95f4\u5904\u7406\u76f8\u5173\u51fd\u6570\u5982time\u3001localtime\u7b49\u3002\u901a\u8fc7\u591a\u4e2a\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u6587\u4ef6\u64cd\u4f5c\u3001\u76ee\u5f55\u904d\u5386\u548c\u65f6\u95f4\u8f6c\u6362\u7b49\u5b9e\u9645\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/56611.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-14T12:45:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda687ce3a.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56611.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/56611.html\",\"name\":\"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-14T12:45:30+00:00\",\"dateModified\":\"2025-08-14T12:45:30+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/56611.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/56611.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/56611.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/56611.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb596\u6b21\uff0c\u70b9\u8d5e24\u6b21\uff0c\u6536\u85cf10\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86Linux\u7cfb\u7edf\u4e2d\u6587\u4ef6IO\u548c\u76ee\u5f55IO\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u64cd\u4f5c\u51fd\u6570\u3002\u6587\u4ef6IO\u90e8\u5206\u8be6\u7ec6\u8bb2\u89e3\u4e86open\u3001close\u3001read\u3001write\u3001lseek\u7b49\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u4ee5\u53ca\u4e0e\u6807\u51c6IO\u7684\u5bf9\u5e94\u5173\u7cfb\uff0c\u5305\u62ec\u6587\u4ef6\u63cf\u8ff0\u7b26\u3001\u6743\u9650\u6807\u5fd7\u7b49\u5173\u952e\u53c2\u6570\u3002\u76ee\u5f55IO\u90e8\u5206\u6db5\u76d6opendir\u3001readdir\u3001closedir\u7b49\u76ee\u5f55\u64cd\u4f5c\u51fd\u6570\uff0c\u4ee5\u53cagetcwd\u3001chdir\u7b49\u8def\u5f84\u7ba1\u7406\u51fd\u6570\u3002\u6587\u7ae0\u8fd8\u5c55\u793a\u4e86\u6df1\u5ea6\u4f18\u5148\u548c\u5e7f\u5ea6\u4f18\u5148\u4e24\u79cd\u76ee\u5f55\u904d\u5386\u7b97\u6cd5\u5b9e\u73b0\uff0c\u5e76\u4ecb\u7ecd\u4e86\u65f6\u95f4\u5904\u7406\u76f8\u5173\u51fd\u6570\u5982time\u3001localtime\u7b49\u3002\u901a\u8fc7\u591a\u4e2a\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u6587\u4ef6\u64cd\u4f5c\u3001\u76ee\u5f55\u904d\u5386\u548c\u65f6\u95f4\u8f6c\u6362\u7b49\u5b9e\u9645","og_url":"https:\/\/www.wsisp.com\/helps\/56611.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-14T12:45:30+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250814124528-689dda687ce3a.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/56611.html","url":"https:\/\/www.wsisp.com\/helps\/56611.html","name":"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-14T12:45:30+00:00","dateModified":"2025-08-14T12:45:30+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/56611.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/56611.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/56611.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u8f6f\u4ef6\u7f16\u7a0b3.\uff08\u6587\u4ef6IO\u548c\u76ee\u5f55IO\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=56611"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/56611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/56605"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=56611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=56611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=56611"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=56611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}