Created
September 10, 2012 07:31
-
-
Save halfelf/3689439 to your computer and use it in GitHub Desktop.
Reading Lines from a File in erlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for_each_line_in_file(Name, Proc, Mode, Accum0) -> | |
{ok, Device} = file:open(Name, Mode), | |
for_each_line(Device, Proc, Accum0). | |
for_each_line(Device, Proc, Accum) -> | |
case io:get_line(Device, "") of | |
eof -> file:close(Device), Accum; | |
Line -> NewAccum = Proc(Line, Accum), | |
for_each_line(Device, Proc, NewAccum) | |
end. | |
% example | |
% A = for_each_line_in_file("complex.erl", | |
% fun(X, Count) -> io:fwrite("~10B: ~s", [Count, X]), | |
% Count + 1 end, [read], 0). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment