(octave.info)EOF and Errors


Next: File Positioning Prev: Temporary Files Up: C-Style I/O Functions
Enter node , (file) or (file)node

14.2.18 End of File and Errors
------------------------------

Once a file has been opened its status can be acquired.  As an example
the ‘feof’ 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.

     filename = "myfile.txt";
     fid = fopen (filename, "r");
     while (! feof (fid) )
       text_line = fgetl (fid);
     endwhile
     fclose (fid);

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.

 -- : STATUS = feof (FID)
     Return 1 if an end-of-file condition has been encountered for the
     file specified by file descriptor FID and 0 otherwise.

     Note that ‘feof’ 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.

     See also: Note: fread, Note: frewind, Note:
     fseek, Note: fclear, Note: fopen.

 -- : MSG = ferror (FID)
 -- : [MSG, ERR] = ferror (FID)
 -- : [...] = ferror (FID, "clear")
     Query the error status of the stream specified by file descriptor
     FID.

     If an error condition exists then return a string MSG describing
     the error.  Otherwise, return an empty string "".

     The second input "clear" is optional.  If supplied, the error state
     on the stream will be cleared.

     The optional second output is a numeric indication of the error
     status.  ERR is 1 if an error condition has been encountered and 0
     otherwise.

     Note that ‘ferror’ indicates if an error has already occurred, not
     whether the next operation will result in an error condition.

     See also: Note: fclear, Note: fopen.

 -- : fclear (FID)
     Clear the stream state for the file specified by the file
     descriptor FID.

     See also: Note: ferror, Note: fopen.

 -- : freport ()
     Print a list of which files have been opened, and whether they are
     open for reading, writing, or both.

     For example:

          freport ()

               ⊣  number  mode  arch       name
               ⊣  ------  ----  ----       ----
               ⊣     0     r    ieee-le    stdin
               ⊣     1     w    ieee-le    stdout
               ⊣     2     w    ieee-le    stderr
               ⊣     3     r    ieee-le    myfile

     See also: Note: fopen, Note: fclose, Note:
     is_valid_file_id.


automatically generated by info2www version 1.2.2.9