{"id":61,"date":"2007-07-18T10:33:17","date_gmt":"2007-07-18T14:33:17","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/index.php\/2007\/07\/18\/a-lesson-well-learned-dont-seek-on-stdin\/"},"modified":"2007-07-18T10:34:51","modified_gmt":"2007-07-18T14:34:51","slug":"a-lesson-well-learned-dont-seek-on-stdin","status":"publish","type":"post","link":"https:\/\/www.virtualroadside.com\/blog\/index.php\/2007\/07\/18\/a-lesson-well-learned-dont-seek-on-stdin\/","title":{"rendered":"A lesson well learned: Don&#8217;t seek() on STDIN"},"content":{"rendered":"<p>The neat thing about fread() and friends in most languages is that you can open up STDIN and pretend its a file. However, the following code doesn&#8217;t work.. hehe.<\/p>\n<pre>&lt;?php\r\n\t$file = $argv[1];\r\n\r\n\tif ($file != '-' &amp;&amp; !file_exists($file))\r\n\t\tdie(\"File $file does not exist, foo!\\n\");\r\n\tif ($file == '-')\r\n\t\t$file = 'php:\/\/stdin';\r\n\r\n\t$f = fopen($file,'r');\r\n\r\n\t\/\/ crazy code in here\r\n\r\n\t\/\/ ok, go backwards\r\n\tfseek($f, $some_position);\r\n\r\n\t\/\/ Heh, it doesn't always work!?\r\n\r\n?&gt;<\/pre>\n<p>Yeah, I know it seems to be quite obvious from this small snippet, but for the record the lines were specified by quite a few lines of code and I didn&#8217;t quite realize that I was doing that&#8230; lol.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The neat thing about fread() and friends in most languages is that you can open up STDIN and pretend its a file. However, the following code doesn&#8217;t work.. hehe. &lt;?php $file = $argv[1]; if ($file != &#8216;-&#8216; &amp;&amp; !file_exists($file)) die(&#8220;File $file does not exist, foo!\\n&#8221;); if ($file == &#8216;-&#8216;) $file = &#8216;php:\/\/stdin&#8217;; $f = fopen($file,&#8217;r&#8217;); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/61"}],"collection":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":0,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}