<html lang="en"> <head> <title>EOF and Errors - 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="Temporary-Files.html#Temporary-Files" title="Temporary Files"> <link rel="next" href="File-Positioning.html#File-Positioning" title="File Positioning"> <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="EOF-and-Errors"></a> <p> Next: <a rel="next" accesskey="n" href="File-Positioning.html#File-Positioning">File Positioning</a>, Previous: <a rel="previous" accesskey="p" href="Temporary-Files.html#Temporary-Files">Temporary Files</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.18 End of File and Errors</h4> <p>Once a file has been opened its status can be acquired. As an example the <code>feof</code> functions determines if the end of the file has been reached. This can be very useful when reading small parts of a file at a time. The following example shows how to read one line at a time from a file until the end has been reached. <pre class="example"> filename = "myfile.txt"; fid = fopen (filename, "r"); while (! feof (fid) ) text_line = fgetl (fid); endwhile fclose (fid); </pre> <p class="noindent">Note that in some situations it is more efficient to read the entire contents of a file and then process it, than it is to read it line by line. This has the potential advantage of removing the loop in the above code. <!-- feof src/file-io.cc --> <p><a name="doc_002dfeof"></a> <div class="defun"> — Built-in Function: <b>feof</b> (<var>fid</var>)<var><a name="index-feof-1015"></a></var><br> <blockquote><p>Return 1 if an end-of-file condition has been encountered for a given file and 0 otherwise. Note that it will only return 1 if the end of the file has already been encountered, not if the next read operation will result in an end-of-file condition. <!-- 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_002dfopen.html#doc_002dfopen">fopen</a>, <a href="doc_002dfclose.html#doc_002dfclose">fclose</a>. </p></blockquote></div> <!-- ferror src/file-io.cc --> <p><a name="doc_002dferror"></a> <div class="defun"> — Built-in Function: [<var>err</var>, <var>msg</var>] = <b>ferror</b> (<var>fid, "clear"</var>)<var><a name="index-ferror-1016"></a></var><br> <blockquote><p>Return 1 if an error condition has been encountered for the file ID <var>fid</var> and 0 otherwise. Note that it will only return 1 if an error has already been encountered, not if the next operation will result in an error condition. <p>The second argument is optional. If it is supplied, also clear the error condition. </p></blockquote></div> <!-- fclear src/file-io.cc --> <p><a name="doc_002dfclear"></a> <div class="defun"> — Built-in Function: <b>fclear</b> (<var>fid</var>)<var><a name="index-fclear-1017"></a></var><br> <blockquote><p>Clear the stream state for the specified file. </p></blockquote></div> <!-- freport src/file-io.cc --> <p><a name="doc_002dfreport"></a> <div class="defun"> — Built-in Function: <b>freport</b> ()<var><a name="index-freport-1018"></a></var><br> <blockquote><p>Print a list of which files have been opened, and whether they are open for reading, writing, or both. For example: <pre class="example"> freport () -| number mode name -| -| 0 r stdin -| 1 w stdout -| 2 w stderr -| 3 r myfile </pre> </blockquote></div> </body></html>