suggestion regarding file:read

94 views
Skip to first unread message

Developer at Works

unread,
Oct 26, 2010, 2:17:05 PM10/26/10
to ce...@googlegroups.com, erlang-prog...@googlegroups.com
Hey Mates

I'm not getting how to solve this file-reading issue.
Exactly, the problem is:-
  I want to read some file from the disk(hard-drive), using multiple-processes, but the use-case restricts not to make any server/loop/receive block/statement.

for ex:- 
test()->
  spawn(?MODULE, test, [?SOURCE_FILE_PATH,0, self()]),
  receive
    M ->
      read(M)   %% THIS STATEMENT IS FAILING.
  end.

test(F, 2, Pid) -> Pid ! F;
test(F, I, Pid)->
  case read(F) of
    eof-> F ! eof; 
    error-> F ! error;
    {FileDescriptor, Data} -> 
      write(?TARGET_FILE_PATH, Data),
      test(FileDescriptor, I+1, Pid)
  end.

read(F)  ->  file:read(F, 1).


ISSUE TRACED SO FAR:
I guess, the problem lies with the change of PID's of the calling process to read/1. Though you are providing correct File-descriptor, it will fail because the  caller-pid changes.


How to solve this, without making server/loop and receive statements either.
--
Thank you
Abhinav

Reza Moussavi

unread,
Oct 26, 2010, 9:31:43 PM10/26/10
to Erlang Programming
fix case of pattern match statements first and try again
let us know the result.

test(F, I, Pid)->
case read(F) of
eof-> F ! eof;
{error, Reason}-> F ! error; %% changed
{ok, Data} -> %% changed
write(?TARGET_FILE_PATH, Data),
test(FileDescriptor, I+1, Pid)
end.

On Oct 26, 8:17 pm, Developer at Works <developeratwo...@gmail.com>
wrote:
> Hey Mates
>
> I'm not getting how to solve this file-reading issue.
> Exactly, the problem is:-
>   I want to read some file from the disk(hard-drive), using
> multiple-processes, but the use-case *restricts not to make any
> server/loop/receive block/statement.*
>
> *for ex:- *
> test()->
>   spawn(?MODULE, test, [?SOURCE_FILE_PATH,0, self()]),
>   receive
>     M ->
>       *read(M) * * %% THIS STATEMENT IS FAILING.*
>   end.
>
> test(F, 2, Pid) -> Pid ! F;
> test(F, I, Pid)->
>   case read(F) of
>     eof-> F ! eof;
>     error-> F ! error;
>     {FileDescriptor, Data} ->
>       write(?TARGET_FILE_PATH, Data),
>       test(FileDescriptor, I+1, Pid)
>   end.
>
> read(F)  ->  file:read(F, 1).
>
> *ISSUE TRACED SO FAR:*

Developer at Works

unread,
Oct 27, 2010, 3:08:27 AM10/27/10
to erlang-prog...@googlegroups.com
Hello Reza

Thank-you for the rectification. You have pointed me correctly, but unfortunately still the problem remains the same. :(

--
Abhinav Mehta

--
Erlang Programming Website:
http://www.erlangprogramming.org/

Developer at Works

unread,
Oct 27, 2010, 5:03:55 AM10/27/10
to erlang-prog...@googlegroups.com, ce...@googlegroups.com
I simplified the problem-statement. Now precisely the problem-statement is:- 

-module(tt).
-compile([export_all]).  %% Line to delete
-define(SOURCE_FILE_PATH, "tt.erl").

test() ->
  {ok, FD} = file:open(?SOURCE_FILE_PATH, [read, raw, binary]),
  io:format("~n * test/1 : ~p~n", [file:read(FD, 10)]),
  spawn(?MODULE, t1, [FD]),
  io:format("~n ** test/1 : ~p~n", [file:read(FD, 10)]).

t1(FD) ->
  R = file:read(FD, 10),
  io:format("~n R: ~p~n", [R]).   % NOT PRINTING, WHY ??
 
Please provide the alternative way, to read the file by using different-process, on the same IODevice.
--
Thank you
Abhinav Mehta

Hynek Vychodil

unread,
Oct 27, 2010, 5:37:21 AM10/27/10
to erlang-prog...@googlegroups.com, ce...@googlegroups.com
Don't use raw. http://erldocs.com/R14B/kernel/file.html?i=8&search=open#open/2

raw
The raw option allows faster access to a file, because no Erlang
process is needed to handle the file. However, a file opened in this
way has the following limitations:

The functions in the io module cannot be used, because they can only
talk to an Erlang process. Instead, use the read/2, read_line/1 and
write/2 functions.
Especially if read_line/1 is to be used on a raw file, it is
recommended to combine this option with the {read_ahead, Size} option
as line oriented I/O is inefficient without buffering.
**Only the Erlang process which opened the file can use it.**
A remote Erlang file server cannot be used; the computer on which the
Erlang node is running must have access to the file system (directly
or through NFS).

1> {ok, F} = file:open("bla.txt", [read, binary]).
{ok,<0.53.0>}
2> P1 = spawn(fun() -> receive _ -> io:format("~p~n", [file:read(F,
10)]) end end).
<0.55.0>
3> P2 = spawn(fun() -> receive _ -> io:format("~p~n", [file:read(F,
10)]) end end).
<0.57.0>
4> P2 ! P1 ! do.
{ok,<<"commit 94c">>}
{ok,<<"28693e1672">>}
do

On Wed, Oct 27, 2010 at 11:03 AM, Developer at Works

--
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com

Hynek Vychodil

unread,
Oct 27, 2010, 5:40:42 AM10/27/10
to erlang-prog...@googlegroups.com

On Wed, Oct 27, 2010 at 11:03 AM, Developer at Works

--

Developer at Works

unread,
Nov 9, 2010, 3:56:47 AM11/9/10
to ce...@googlegroups.com, erlang-prog...@googlegroups.com
Thank you Scott and Hynek Vychodil. 
Infact, Hynek your example works like a charm. :)

--
Abhinav Mehta


On Wed, Nov 3, 2010 at 8:51 AM, Scott Fritchie <slfri...@gmail.com> wrote:
Developer at Works <develope...@gmail.com> wrote:

daw> I simplified the problem-statement. Now precisely the
daw> problem-statement is:-

Please see the OTP reference for the file:open/2 function's 'raw'
option:

   http://www.snookles.com/erlang-docs/R13B04/lib/kernel-2.13.5/doc/html/file.\
html#open-2


   The raw option allows faster access to a file, because no Erlang
   process is needed to handle the file. However, a file opened in this
   way has the following limitations:
       [...]
       * Only the Erlang process which opened the file can use it.

-Scott

--
You received this message because you are subscribed to the Google Groups "Chicago Erlang User Group" group.
To post to this group, send email to ce...@googlegroups.com.
To unsubscribe from this group, send email to ceug+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ceug?hl=en.


Reply all
Reply to author
Forward
0 new messages