<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.fscanf.php',
    1 => 'fscanf',
    2 => 'Parses input from a file according to a format',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.fread.php',
    1 => 'fread',
  ),
  'next' => 
  array (
    0 => 'function.fseek.php',
    1 => 'fseek',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/fscanf.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.fscanf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fscanf</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fscanf</span> &mdash; <span class="dc-title">Parses input from a file according to a format</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fscanf-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fscanf</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.string.php" class="type string">string</a></span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter reference">&...$vars</code></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span></div>

  <p class="para rdfs-comment">
   The function <span class="function"><strong>fscanf()</strong></span> is similar to
   <span class="function"><a href="function.sscanf.php" class="function">sscanf()</a></span>, but it takes its input from a file
   associated with <code class="parameter">stream</code> and interprets the
   input according to the specified <code class="parameter">format</code>. 
  </p>
  <p class="para">
   Any whitespace in the format string matches any whitespace in the input
   stream. This means that even a tab (<code class="literal">\t</code>) in the format
   string can match a single space character in the input stream.
  </p>
  <p class="para">
   Each call to <span class="function"><strong>fscanf()</strong></span> reads one line from the file.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fscanf-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">stream</code></dt>
     <dd>
      <p class="para">A file system pointer <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>
that is typically created using <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span>.</p>
     </dd>
    
    

     <dt><code class="parameter">format</code></dt>
     <dd>
      <p class="para">
       The interpreted format for <code class="parameter">string</code>, which is
       described in the documentation for <span class="function"><a href="function.sprintf.php" class="function">sprintf()</a></span> with
       following differences:
       <ul class="simplelist">
        <li>
         Function is not locale-aware.
        </li>
        <li>
         <code class="literal">F</code>, <code class="literal">g</code>, <code class="literal">G</code> and
         <code class="literal">b</code> are not supported.
        </li>
        <li>
         <code class="literal">D</code> stands for decimal number.
        </li>
        <li>
         <code class="literal">i</code> stands for integer with base detection.
        </li>
        <li>
         <code class="literal">n</code> stands for number of characters processed so far.
        </li>
        <li>
         <code class="literal">s</code> stops reading at any whitespace character.
        </li>
        <li>
         <code class="literal">*</code> instead of <code class="literal">argnum$</code> suppresses
         the assignment of this conversion specification.
        </li>
       </ul>
      </p>
     </dd>
    

    
     <dt><code class="parameter">vars</code></dt>
     <dd>
      <p class="para">
       The optional assigned values.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fscanf-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   If only two parameters were passed to this function, the values parsed will be
   returned as an array. Otherwise, if optional parameters are passed, the
   function will return the number of assigned values. The optional
   parameters must be passed by reference. 
  </p>
  <p class="para">
   If there are more substrings expected in the <code class="parameter">format</code>
   than there are available within <code class="parameter">string</code>,
   <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> will be returned. On other errors, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> will be returned.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fscanf-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>fscanf()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$handle </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"users.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$userinfo </span><span style="color: #007700">= </span><span style="color: #0000BB">fscanf</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">, </span><span style="color: #DD0000">"%s\t%s\t%s\n"</span><span style="color: #007700">)) {<br />    list (</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$profession</span><span style="color: #007700">, </span><span style="color: #0000BB">$countrycode</span><span style="color: #007700">) = </span><span style="color: #0000BB">$userinfo</span><span style="color: #007700">;<br />    </span><span style="color: #FF8000">//... do something with the values<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 Contents of users.txt</strong></p>
    <div class="example-contents">
<div class="txtcode"><pre class="txtcode">javier  argonaut        pe
hiroshi sculptor        jp
robert  slacker us
luigi   florist it</pre>
</div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-function.fscanf-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li><span class="function"><a href="function.fgetss.php" class="function" rel="rdfs-seeAlso">fgetss()</a> - Gets line from file pointer and strip HTML tags</span></li>
    <li><span class="function"><a href="function.sscanf.php" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li>
    <li><span class="function"><a href="function.printf.php" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li>
    <li><span class="function"><a href="function.sprintf.php" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
   </ul>
  </p>
 </div>


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