<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.fseek.php',
    1 => 'fseek',
    2 => '在文件指针中定位',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => '文件系统函数',
  ),
  'prev' => 
  array (
    0 => 'function.fscanf.php',
    1 => 'fscanf',
  ),
  'next' => 
  array (
    0 => 'function.fstat.php',
    1 => 'fstat',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/filesystem/functions/fseek.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fseek" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fseek</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fseek</span> &mdash; <span class="dc-title">在文件指针中定位</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fseek-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fseek</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> <code class="parameter">$stream</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$whence</code><span class="initializer"> = <strong><code><a href="filesystem.constants.php#constant.seek-set">SEEK_SET</a></code></strong></span></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   在与 <code class="parameter">stream</code> 关联的文件中设定文件指针位置。新位置从文件头开始以字节数度量，是以
   <code class="parameter">whence</code> 指定的位置加上 <code class="parameter">offset</code>。
  </p>
  <p class="para">
   一般来说，允许定位超过文件末尾的位置；如果随后写入数据，则从文件末尾和定位位置之间已读取的任何未写入区域将产生值为 0
   的字节。但是，某些 stream 可能不支持此行为，尤其是当它们具有底层固定大小的存储时。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fseek-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>(资源)。</p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       偏移量。
      </p>
      <p class="para">
       要移动到文件尾之前的位置，必须传递负数值给 <code class="parameter">offset</code>，而且
       <code class="parameter">whence</code> 参数设置为 <strong><code><a href="filesystem.constants.php#constant.seek-end">SEEK_END</a></code></strong>。
      </p>
     </dd>
    
    
     <dt><code class="parameter">whence</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">whence</code> 值是：
       <ul class="simplelist">
        <li><strong><code><a href="filesystem.constants.php#constant.seek-set">SEEK_SET</a></code></strong> - 设定位置等于距文件开头为 <code class="parameter">offset</code> 字节。</li>
        <li><strong><code><a href="filesystem.constants.php#constant.seek-cur">SEEK_CUR</a></code></strong> - 设定位置为当前位置加上 <code class="parameter">offset</code> 字节。</li>
        <li><strong><code><a href="filesystem.constants.php#constant.seek-end">SEEK_END</a></code></strong> - 设定位置为文件尾加上 <code class="parameter">offset</code> 字节。</li>
       </ul>
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fseek-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    成功返回 <code class="literal">0</code>；否则返回 <code class="literal">-1</code>。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    此函数是为了模仿 C 语言中同名函数而创建的。请注意返回值，因为它们与 PHP 中的期望值不同。
   </p>
  </div>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fseek-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>fseek()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// read some data<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">4096</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// move back to the beginning of the file<br />// same as rewind($fp);<br /></span><span style="color: #0000BB">fseek</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.fseek-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果使用附加模试（<code class="literal">a</code> 或
    <code class="literal">a+</code>），任何写入文件数据都会被附加上去，而文件的位置将会被忽略，调用
    <span class="function"><strong>fseek()</strong></span> 的结果尚未定义。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    并非所有 stream 都支持定位。对于不支持定位的 stream，从当前位置向前定位是通过读取和丢弃数据来实现的；其他形式的定位将会失败。
   </p>
  </p></blockquote>
 </div>

 
  <div class="refsect1 seealso" id="refsect1-function.fseek-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.ftell.php" class="function" rel="rdfs-seeAlso">ftell()</a> - 返回文件指针读/写的位置</span></li>
    <li><span class="function"><a href="function.rewind.php" class="function" rel="rdfs-seeAlso">rewind()</a> - 倒回文件指针的位置</span></li>
   </ul>
  </p>
 </div>

 
</div><?php manual_footer($setup); ?>