[erlang-questions] Package Support/Use

Richard A. O'Keefe ok@REDACTED
Thu Nov 9 03:27:58 CET 2006

I wrote:
    > I'm sure you will appreciate why I am so firmly convinced that end 
    > of file should not be regarded as exceptional.

Richard Carlsson <richardc@REDACTED> replied:
    As Ulf (I think) noted, perhaps there should really be two different 
    interfaces: one for random reading, where you expect the data to be 
    there (and it is an exception if you reach eof), and one for sequential 
    reading, returning {read, X}/eof.
But there *ARE* two different interfaces already!
And they *ARE* different in exactly that way!

For sequential reading we have

    file:read(Device, Count) -> {ok,Data} | eof | {error,Reason}

For random reading we have

    file:pread(Device, Location, Count) -> {ok,Data} | {error,Reason}

As usual, the documentation could stand some work.
The documentation of file:pread/3 says

    Combines position/2 and read/2 in one operation, which is more
    efficient than calling them one at a time.   If Device [was]
    opened in raw mode ... Location [may] only be an integer, and
    the current position of the file is undefined after the operation.

But if you did
    case file:position(Device, Location)
     of  {ok,_} -> file:read(Device, Count)
      ;  Error  -> Error
then 'eof' would be a possible result of file:pread/3, which it isn't.
Nor is it clear what happens if you ask for more bytes than are available;
*presumably* you get a short block.

More information about the erlang-questions mailing list