<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 * Copyright (C) 2010 - Digiteo - Vincent LIARD
 * ...
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns4="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="mgetstr" xml:lang="en">
    <refnamediv>
        <refname>mgetstr</refname>
        <refpurpose>reads a character string from a file</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>str = mgetstr(n, [fd])</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>n</term>
                <listitem>
                    <para>non-negative integer: how many characters should be
                        read.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>fd</term>
                <listitem>
                    <para>
                        an integer: file descriptor as returned by
                        <link linkend="mopen">mopen</link>: where characters should be read. The value <literal>-1</literal> stands for the last opened file and is the default.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>str</term>
                <listitem>
                    <para>the character string to be read.</para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            The <function>mgetstr</function> function attempts to read up
            to <varname>n</varname> characters from a file. If end of file is
            reached before <varname>n</varname> characters are read,
            <function>mgetstr</function> returns the properly read values
            only. As a consequence, when the read cursor is already at the end
            of the file when <function>mgetstr</function> is called, it returns
            an empty string no matter what value is provided for
            <varname>n</varname>.
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
      fn = SCI + '/ACKNOWLEDGEMENTS'; // absolute path to some file
      details = fileinfo(fn);         // retrieve file details
      len = details(1);               // get file length
      fd = mopen(fn, 'rt');           // open file as text with read mode
      str1 = mgetstr(33, fd)          // read 33 characters from fd
      length(str1)                    // 33 characters read
      str2 = mgetstr(272, fd)         // read the next 272 characters from fd
      length(str2)                    // 272 characters read
      mseek(len - 5);                 // jump to the 5th character before end of file
      str3 = mgetstr(10, fd)          // try to read 10 characters: returns 5 characters only, no more available
      length(str3)                    // yes, there are 5 characters: you just can't see the linefeed :)
      str4 = mgetstr(10, fd)          // read 10 characters: returns empty string
      length(str4)                    // empty string
      mclose(fd);                     // close file descriptor
  ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="mclose">mclose</link>
            </member>
            <member>
                <link linkend="meof">meof</link>
            </member>
            <member>
                <link linkend="mfprintf">mfprintf</link>
            </member>
            <member>
                <link linkend="fprintfMat">fprintfMat</link>
            </member>
            <member>
                <link linkend="mfscanf">mfscanf</link>
            </member>
            <member>
                <link linkend="fscanfMat">fscanfMat</link>
            </member>
            <member>
                <link linkend="mget">mget</link>
            </member>
            <member>
                <link linkend="mopen">mopen</link>
            </member>
            <member>
                <link linkend="mprintf">mprintf</link>
            </member>
            <member>
                <link linkend="mput">mput</link>
            </member>
            <member>
                <link linkend="mputstr">mputstr</link>
            </member>
            <member>
                <link linkend="mseek">mseek</link>
            </member>
            <member>
                <link linkend="mtell">mtell</link>
            </member>
            <member>
                <link linkend="mdelete">mdelete</link>
            </member>
            <member>"Input/Output functions" section</member>
            <!-- TODO: link to "Input/Output functions" index page -->
            <!-- next link does not work when reading doc in scilab -->
            <!-- <member><link linkend="section_efa4cbf5c15e166f34901f827390e756">Input/Output functions</link></member> -->
        </simplelist>
    </refsection>
</refentry>
