{"id":27500,"date":"2024-11-24T12:50:27","date_gmt":"2024-11-24T04:50:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27500\/"},"modified":"2024-11-24T12:50:27","modified_gmt":"2024-11-24T04:50:27","slug":"php%e4%b8%ad%e7%9a%84%e8%be%93%e5%85%a5%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27500\/","title":{"rendered":"php\u4e2d\u7684\u8f93\u5165\u8bed\u53e5\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  php \u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u5165\u8bed\u53e5\u6765\u4ece\u7528\u6237\u83b7\u53d6\u6570\u636e\uff1afgets()\uff1a\u9010\u884c\u8bfb\u53d6\u8f93\u5165fgetc()\uff1a\u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165fread()\uff1a\u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165feof()\uff1a\u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3efwrite()\uff1a\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/11\/2024111100275944915.jpg\" class=\"aligncenter\" title=\"php\u4e2d\u7684\u8f93\u5165\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"php\u4e2d\u7684\u8f93\u5165\u8bed\u53e5\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u7684\u8f93\u5165\u8bed\u53e5<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u5165\u8bed\u53e5\u6765\u4ece\u7528\u6237\u83b7\u53d6\u6570\u636e\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>fgets()<\/strong>\uff1a\u9010\u884c\u8bfb\u53d6\u8f93\u5165<\/li>\n<li> <strong>fgetc()<\/strong>\uff1a\u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165<\/li>\n<li> <strong>fread()<\/strong>\uff1a\u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165<\/li>\n<li> <strong>feof()<\/strong>\uff1a\u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e<\/li>\n<li> <strong>fwrite()<\/strong>\uff1a\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<p>\u8f93\u5165\u8bed\u53e5\u7684\u8bed\u6cd5\u4e00\u822c\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>&lt;\u8bed\u53e5&gt;(&lt;\u6587\u4ef6\u6307\u9488&gt;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> \u662f\u8f93\u5165\u8bed\u53e5\u672c\u8eab\uff0c\u5982 fgets()\u3001fgetc() \u7b49<\/li>\n<li> \u662f\u6307\u5411\u6587\u4ef6\u7684\u6587\u4ef6\u53e5\u67c4<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u4f7f\u7528 fgets() \u8bfb\u53d6\u4e00\u884c\u8f93\u5165\uff1a<\/strong><\/p>\n<pre>$handle = fopen(\"input.txt\", \"r\");\n$line = fgets($handle);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 fgetc() \u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165\uff1a<\/strong><\/p>\n<pre>$handle = fopen(\"input.txt\", \"r\");\nwhile (!feof($handle)) {\n  $char = fgetc($handle);\n  echo $char;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 fread() \u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165\uff1a<\/strong><\/p>\n<pre>$handle = fopen(\"input.txt\", \"r\");\n$data = fread($handle, 1024); \/\/ \u8bfb\u53d6 1024 \u5b57\u8282<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 feof() \u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\uff1a<\/strong><\/p>\n<pre>$handle = fopen(\"input.txt\", \"r\");\nwhile (!feof($handle)) {\n  $line = fgets($handle);\n  echo $line;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fphp\u4e2d\u7684\u8f93\u5165\u8bed\u53e5\u600e\u4e48\u5199\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php \u4e2d\u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u5165\u8bed\u53e5\u6765\u4ece\u7528\u6237\u83b7\u53d6\u6570\u636e\uff1afgets()\uff1a\u9010\u884c\u8bfb\u53d6\u8f93\u5165fgetc()\uff1a\u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165fread()\uff1a\u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165feof()\uff1a\u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3efwrite()\uff1a\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e PHP \u4e2d\u7684\u8f93\u5165\u8bed\u53e5 PHP \u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u5165\u8bed\u53e5\u6765\u4ece\u7528\u6237\u83b7\u53d6\u6570\u636e\uff0c\u5305\u62ec\uff1a fgets()\uff1a\u9010\u884c\u8bfb\u53d6\u8f93\u5165 fgetc()\uff1a\u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165 fread()\uff1a\u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165 feof()\uff1a\u68c0\u67e5\u6587\u4ef6\u6307\u9488\u662f\u5426\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e fwrite()\uff1a\u5411\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e \u8bed\u6cd5 \u8f93\u5165\u8bed\u53e5\u7684\u8bed\u6cd5\u4e00\u822c\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b &lt;\u8bed\u53e5&gt;(&lt;\u6587\u4ef6\u6307\u9488&gt;) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a \u662f\u8f93\u5165\u8bed\u53e5\u672c\u8eab\uff0c\u5982 fgets()\u3001fgetc() \u7b49 \u662f\u6307\u5411\u6587\u4ef6\u7684\u6587\u4ef6\u53e5\u67c4 \u793a\u4f8b \u4f7f\u7528 fgets() \u8bfb\u53d6\u4e00\u884c\u8f93\u5165\uff1a $handle = fopen(&#8220;input.txt&#8221;, &#8220;r&#8221;); $line = fgets($handle); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 fgetc() \u9010\u4e2a\u5b57\u7b26\u8bfb\u53d6\u8f93\u5165\uff1a $handle = fopen(&#8220;input.txt&#8221;, &#8220;r&#8221;); while (!feof($handle)) { $char = fgetc($handle); echo $char; } \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 fread() \u8bfb\u53d6\u6307\u5b9a\u957f\u5ea6\u5b57\u8282\u7684\u8f93\u5165\uff1a $handle = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-27500","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=27500"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27500\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}