<html lang="en"> <head> <title>Line-Oriented Input - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions" title="C-Style I/O Functions"> <link rel="prev" href="Simple-Output.html#Simple-Output" title="Simple Output"> <link rel="next" href="Formatted-Output.html#Formatted-Output" title="Formatted Output"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Line-Oriented-Input"></a> <a name="Line_002dOriented-Input"></a> <p> Next: <a rel="next" accesskey="n" href="Formatted-Output.html#Formatted-Output">Formatted Output</a>, Previous: <a rel="previous" accesskey="p" href="Simple-Output.html#Simple-Output">Simple Output</a>, Up: <a rel="up" accesskey="u" href="C_002dStyle-I_002fO-Functions.html#C_002dStyle-I_002fO-Functions">C-Style I/O Functions</a> <hr> </div> <h4 class="subsection">14.2.3 Line-Oriented Input</h4> <p>To read from a file it must be opened for reading using <code>fopen</code>. Then a line can be read from the file using <code>fgetl</code> as the following code illustrates <pre class="example"> fid = fopen ("free.txt"); txt = fgetl (fid) -| Free Software is needed for Free Science fclose (fid); </pre> <p class="noindent">This of course assumes that the file ‘<samp><span class="samp">free.txt</span></samp>’ exists and contains the line ‘<samp><span class="samp">Free Software is needed for Free Science</span></samp>’. <!-- fgetl src/file-io.cc --> <p><a name="doc_002dfgetl"></a> <div class="defun"> — Built-in Function: <b>fgetl</b> (<var>fid, len</var>)<var><a name="index-fgetl-984"></a></var><br> <blockquote><p>Read characters from a file, stopping after a newline, or EOF, or <var>len</var> characters have been read. The characters read, excluding the possible trailing newline, are returned as a string. <p>If <var>len</var> is omitted, <code>fgetl</code> reads until the next newline character. <p>If there are no more characters to read, <code>fgetl</code> returns −1. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dfread.html#doc_002dfread">fread</a>, <a href="doc_002dfscanf.html#doc_002dfscanf">fscanf</a>. </p></blockquote></div> <!-- fgets src/file-io.cc --> <p><a name="doc_002dfgets"></a> <div class="defun"> — Built-in Function: <b>fgets</b> (<var>fid</var>)<var><a name="index-fgets-985"></a></var><br> — Built-in Function: <b>fgets</b> (<var>fid, len</var>)<var><a name="index-fgets-986"></a></var><br> <blockquote><p>Read characters from a file, stopping after a newline, or EOF, or <var>len</var> characters have been read. The characters read, including the possible trailing newline, are returned as a string. <p>If <var>len</var> is omitted, <code>fgets</code> reads until the next newline character. <p>If there are no more characters to read, <code>fgets</code> returns −1. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dfputs.html#doc_002dfputs">fputs</a>, <a href="doc_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfread.html#doc_002dfread">fread</a>, <a href="doc_002dfscanf.html#doc_002dfscanf">fscanf</a>. </p></blockquote></div> <!-- fskipl src/file-io.cc --> <p><a name="doc_002dfskipl"></a> <div class="defun"> — Built-in Function: <b>fskipl</b> (<var>fid, count</var>)<var><a name="index-fskipl-987"></a></var><br> <blockquote><p>Skip a given number of lines, i.e., discards characters until an end-of-line is met exactly <var>count</var>-times, or end-of-file occurs. Returns the number of lines skipped (end-of-line sequences encountered). If <var>count</var> is omitted, it defaults to 1. <var>count</var> may also be <code>Inf</code>, in which case lines are skipped to the end of file. This form is suitable for counting lines in a file. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dfgetl.html#doc_002dfgetl">fgetl</a>, <a href="doc_002dfgets.html#doc_002dfgets">fgets</a>. </p></blockquote></div> </body></html>