[erlang-questions] file:read_file won't read from /proc

Jachym Holecek freza@REDACTED
Fri Mar 5 15:51:42 CET 2010


# Martin DeMello 2010-03-05:
> On Thu, Mar 4, 2010 at 9:37 PM, Jachym Holecek <freza@REDACTED> wrote:
> 
> > The read_file/1 thing check file size and then attempts to read exactly
> > that many bytes. The size of /proc/cpuinfo is zero[*], thus read_file/1
> > correctly reads zero bytes and represents the result with empty binary.
> 
> Okay, that makes sense. So what would the best way to read "files" in
> /proc be? Is there any standard erlang way to read a stream till you
> get an EOF?

Not sure if there's such function in standard libraries, you can always
go with a utility function much in the spirit of your original example:

  raw_read_file(Path) ->
      {ok, File} = file:open(Path, [read, binary]),
      raw_read_loop(File, []).

  raw_read_loop(File, Acc) ->
      case file:read(File, 1024) of
        {ok, Bytes} ->
            raw_read_loop(File, [Acc | Bytes]);
        eof ->
	    file:close(File),
            iolist_to_binary(Acc);
	{error, Reason} ->
	    file:close(File),
	    erlang:error(Reason)
      end.

Regards,
	-- Jachym


More information about the erlang-questions mailing list